https://github.com/Asmod4n/mruby-sysrandom
Secure random number generation for mruby
https://github.com/Asmod4n/mruby-sysrandom
mruby random-number-generators rng
Last synced: 6 months ago
JSON representation
Secure random number generation for mruby
- Host: GitHub
- URL: https://github.com/Asmod4n/mruby-sysrandom
- Owner: Asmod4n
- License: other
- Created: 2016-05-30T16:33:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-06-23T11:23:38.000Z (almost 6 years ago)
- Last Synced: 2024-11-12T17:48:58.588Z (6 months ago)
- Topics: mruby, random-number-generators, rng
- Language: C
- Homepage:
- Size: 14.6 KB
- Stars: 2
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-mruby - mruby-sysrandom - Secure random number generation for mruby. (Utilities)
README
# mruby-sysrandom
Secure random number generation for mruby using system RNG facilities e.g. /dev/urandom, getrandom(2)
## Description
In cryptography circles, [the prevailing advice is to use OS RNG functionality][/dev/urandom],
namely `/dev/urandom` or equivalent calls which use an OS-level CSPRNG to
produce random numbers.This gem provides an easy-to-install repackaging of the `randombytes`
functionality from [libsodium] for the purpose of generating secure random
numbers trustworthy for use in cryptographic contexts, such as generating
cryptographic keys, initialization vectors, or nonces.The following random number generators are utilized:
| Platform | RNG |
|----------|--------------------------------------------------------|
| Linux | [getrandom(2)] if available, otherwise [/dev/urandom] |
| Windows | [RtlGenRandom] CryptGenRandom without CryptoAPI deps |
| OpenBSD | [arc4random(3)] with ChaCha20 CSPRNG (not RC4) |
| Others | [/dev/urandom] |[emboss]: https://emboss.github.io/blog/2013/08/21/openssl-prng-is-not-really-fork-safe/
[bug]: https://bugs.ruby-lang.org/issues/9569
[libsodium]: https://github.com/jedisct1/libsodium
[getrandom(2)]: http://man7.org/linux/man-pages/man2/getrandom.2.html
[/dev/urandom]: http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
[RtlGenRandom]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa387694(v=vs.85).aspx
[arc4random(3)]: http://man.openbsd.org/arc4random.3## Usage
```ruby
Sysrandom.random_bytes # returns a 16 byte binary stringSysrandom.random_bytes(64)
Sysrandom.random_bytes(" " * 10)
Sysrandom.random # returns a number
Sysrandom.uniform(upper_bound) # returns a number up to upper_bound
Sysrandom.base64 # returns a 16 byte binary string as base64
Sysrandom.base64(64)
Sysrandom.base64(" " * 10)
Sysrandom.hex # returns a 16 byte binary string as hex
Sysrandom.hex(64)
Sysrandom.hex(" " * 10)
```