Cryptographically secure pseudo-random number generation
To use this package, run the following command in your project's root directory:
Put the following dependency into your project's dependences section:
CSPRNG D Library
- Author: Jonathan M. Wilbur [email@example.com](mailto:firstname.lastname@example.org)
- Copyright Year: 2018
- License: MIT License
- Version: 1.1.0
What Is This Library?
This is a library for generating cryptographically random numbers. The acronym, CSPRNG, stands for "Cryptographically-Secure Pseudo-Random Number Generator." This library relies upon existing system sources for cryptographic randomness.
When compiled on Windows, it uses the
RtlGenRandom system APIs. When compiled on Linux, FreeBSD, or DragonFlyBSD,
it reads random bytes from
/dev/random. When compiled on Mac OS X, OpenBSD,
or NetBSD, it reads random bytes from the
arc4random_buf function in the
C runtime library. Regardless of underlying operating system, it resorts to
arc4random_buf function if the Bionic C Library is in use.
Building and Installing
There are four scripts in
build/scripts that help you build this library,
in addition to building using
dub. If you are using Windows, you can build
.\build\scripts\build.ps1 from PowerShell, or
from the traditional
cmd shell. If you are on any POSIX-compliant(-ish)
operating system, such as Linux or Mac OS X, you may build this library using
make -f ./build/scripts/posix.make. The output
library will be in
./build/libraries. The command-line tools will be in
For more information on building and installing, see
The usage of this library is really straight-forward. Whether you are on Windows, Mac OS X, or Linux, generating cryptographically-secure random bytes looks like this:
import csprng.system; CSPRNG c = new CSPRNG(); writeln(c.getBytes(10)); // Writes ten random bytes to the command line.
Alternatively, you can get random numeric data types, including static arrays of numeric types like so:
import csprng.system; CSPRNG c = new CSPRNG(); writeln(c.get!(int)()); // Writes an int where each int is random.
On the backend, this library handles opening, caching, and closing file descriptors, and efficiently managing the Windows cryptography API constructs, so you don't have to!
CSPRNG object fails to load the system APIs for generating secure
random bytes, it will throw a
Command-Line Tool Usage
This library comes with a simple command-line tool for generating secure
random bytes, called
get-cryptobytes. It is used like so:
It takes only a single argument, specifying the number of random bytes wanted.
Though this library is ready for production, there are a few changes I plan to make:
- [x] Implement a
gettemplated method that retrieves a random equivalent of any parameterized numeric data type.
- [ ] Make
SecureARC4Randomimplementations fall back on
SecureARC4Randomis not present.
- [ ] At least include assembly files for
- "ARC4" is a trademark of RSA Laboratories.
If you would like to suggest fixes or improvements on this library, please just leave an issue on this GitHub page. If you would like to contact me for other reasons, please email me at email@example.com (My GPG Key) (My TLS Certificate). :boar:
- Registered by Jonathan M. Wilbur
- 1.1.0 released 4 years ago