https://github.com/mackron/cryptorand
Cryptographically Secure Pseudo-Random Number Generator.
https://github.com/mackron/cryptorand
random
Last synced: about 1 year ago
JSON representation
Cryptographically Secure Pseudo-Random Number Generator.
- Host: GitHub
- URL: https://github.com/mackron/cryptorand
- Owner: mackron
- Created: 2022-02-06T08:20:07.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-07T11:24:16.000Z (about 4 years ago)
- Last Synced: 2025-03-25T02:36:41.131Z (about 1 year ago)
- Topics: random
- Language: C
- Homepage:
- Size: 22.5 KB
- Stars: 12
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Cryptographically Secure Pseudo-Random Number Generator
This uses the operating system's random number generation. If you're looking for a CSPRNG from
scratch you'll need to look elsewhere.
Supported generation methods are Win32's BCryptGenRandom() with CryptGenRandom() as a fallback. On
platforms that support /dev/urandom, that will be used. OpenBSD will use arc4random().
There is no need to link to anything with this library. You can use CRYPTORAND_IMPLEMENTATION to
define the implementation section, or you can use cryptorand.c if you prefer a traditional
header/source pair.
There's only three functions, all of which should be self explanatory and easy to figure out:
cryptorand_result cryptorand_init(cryptorand* pRNG);
void cryptorand_uninit(cryptorand* pRNG);
cryptorand_result cryptorand_generate(cryptorand* pRNG, void* pBufferOut, size_t byteCount);
Call `cryptorand_init()` to initialize the random number generator. On Windows, this is where
libraries are linked at runtime so avoid calling this in high performance scenarios. It's best to
just create one instance and then read from it multiple times.
To generate random bytes you need only call `cryptorand_generate()`. You just specify a pointer to
a buffer that will receive the random data and the number of bytes you want. If this fails, the
content of the buffer will be cleared to zero.
Uninitialize the random number generator with `cryptorand_uninit()`.
Thread safety depends on the backend.