A cryptographically secure pseudo-random number generator CLI tool which generates byte arrays with entropy from the best random source from your machine* optionally externally seeded by multiple true random number generators and supports various byte-to-text encodings like hex or base64 and for many programming languages. The output may be printed to the command line or to a file. This implementation uses the HMAC Deterministic Random Bit Generator (DRBG) schema as defined in NIST SP800-90Ar1.
* depending on the used provider
Example usage generating randoms with 24 byte-length (not char length) and default encoding:
1java -jar dice.jar 24
1java -jar dice.jar 16 --count 1002java -jar dice.jar 16 --encoding "base64"3java -jar dice.jar 16 --encoding "java"4java -jar dice.jar 4096 --encoding "raw" --count 1024 --file "./rnd-4-MiB-outputfile.txt"5java -jar dice.jar 16 --seed "myBadRandomSeed"6java -jar dice.jar 16 --offline7java -jar dice.jar 32 --encoding "base85" --urlencode --padding --crc32
This should run on any Windows, Mac or Linux machine.
Using the *.exe Launcher: Launch4J is used to wrap the
.jar into an Windows executable. It should automatically download the needed JRE if required.
base58 because these encodings are typically url-safe. 16 byte usually suffice for globally unique, infeasible to brute force number.
1java -jar dice.jar 16 -e "base36"
If you require fixed char-sized output either use
hex encoding or other encodings supporting paddings like
base64. For passwords, high-density encodings are recommended like
You can create static salts, or randoms to hardcode, in your code. Just pick your programming language to get the correct syntax (see below). E.g.:
1java -jar dice.jar 16 -e "java"
Create a file 4MiB full of raw random bytes with this call:
1java -jar dice.jar 4096 -c 1024 -e "raw" -f "./rnd-outfile.txt"
This will create random chunks of 4Kib (the maximum allowed size per chunk) repeated 1024 times. Currently the tool is capped to 10 GiB of generated random data per call. Successive calls will append data, not overwrite it.
1 --anuquantum Enable external, supposed true random generator ANU Quantum; note this service is known2 to be slow (only when online).3-c,--count
How many randoms should be generated. Automatically chosen if this argument is omitted.4 --crc32 If this flag is set, 4 bytes of CRC32 checksum will be appended to every random value.5 If you need to check the integrity of the data.6-d,--debug Prints additional info for debugging.7-e,--encoding Output byte-to-text encoding. Available encodings include:8 binary, octal, dec, base16, BASE16, base26, base32, base36, base58, base64, base64-url,9 base85, c, c#, java, go, kotlin, node, js, perl, php, python3, ruby, rust, swift, img,10 raw, utf811-f,--file Prints the random data to given file instead of the command line. Will create the file12 if it does not exist or append the data if it does.13-h,--help Shows this page.14-o,--offline Skips request to external random generators (random.org & hotbits) for seeding (use when15 offline).16-p,--padding If this flag is set, byte-to-text output will be padded to full byte if needed.17-r,--robot If this flag is set, output will be more friendly for scripting (ie. no verbose text,18 only the randoms 1 per line)19-s,--seed Uses either the 64-bit integer interpretation or the utf-8 byte representation of given20 parameter to seed the internal random generator. Warns if entropy is low.21-u,--urlencode Uses 'www-form-urlencoded' encoding scheme, also misleadingly known as URL encoding, on22 the output strings23-v,--version Prints application version.
|binary||12.5 %||false||A simple binary representation with '0' and '1' divided into 8 bit groups.|
|octal||37.5 %||true||The octal numeral system, is the base-8 number system, and uses the digits 0 to 7.|
|dec||41.5 %||true||Decimal positive sign-magnitude representation representation in big-endian byte-order.|
|base16||50.0 %||false||Base16 or hex stores each byte as a pair of hexadecimal digits. Lowercase (a-f) letters are used for digits greater than 9.|
|BASE16||50.0 %||false||Base16 or hex stores each byte as a pair of hexadecimal digits. Uppercase (A-F) letters are used for digits greater than 9.|
|base26||58.8 %||true||Base26 uses the twenty-six letters A-Z.|
|base32||62.5 %||true||Base32 uses a 32-character subset of the twenty-six letters A-Z and the digits 2-7. Uses the alphabet defined in RFC 4648.|
|base36||64.6 %||true||Base36 translating into a radix-36 (aka Hexatrigesimal) representation.|
|base58||73.2 %||true||Base58 is similar to Base64 but has been modified to avoid both non-alphanumeric characters and letters which might look ambiguous when printed. This version uses the alphabet common for Bitcoin protocol.|
|base64||75.0 %||true||Base64 represent binary data in an ASCII string format by translating it into a radix-64 representation.|
|base64-url||75.0 %||true||Base64 represent binary data in an ASCII string format by translating it into a radix-64 representation. Uses url safe mode|
|base85||80.1 %||true||Base85 uses an 85 character ASCII alphabet to encode. It's main use is with the PDF format and GIT.|
|raw||Prints the raw byte array encoded in ISO_8859_1 which does not change the byte output. Most useful with file output.|
|utf8||Prints the byte array interpreted as UTF-8 encoded text. Only for testing purpose.|
|img||Prints a byte per character encoded in unicode block elements.|
The provided JARs in the Github release page are signed with my private key:
1CN=Patrick Favre-Bulle, OU=Private, O=PF Github Open Source, L=Vienna, ST=Vienna, C=AT2Validity: Thu Sep 07 16:40:57 SGT 2017 to: Fri Feb 10 16:40:57 SGT 20343SHA1: 06:DE:F2:C5:F7:BC:0C:11:ED:35:E2:0F:B1:9F:78:99:0F:BE:43:C44SHA256: 2B:65:33:B0:1C:0D:2A:69:4E:2D:53:8F:29:D5:6C:D6:87:AF:06:42:1F:1A:EE:B3:3C:E0:6D:0B:65:A1:AA:88
Use the jarsigner tool (found in your
$JAVA_HOME/bin folder) folder to verify.
All tags and commits by me are signed with git with my private key:
1GPG key ID: 4FDF85343912A3AB2Fingerprint: 2FB392FB05158589B767960C4FDF85343912A3AB
As cryptographically secure pseudorandom number generator, the NIST SP800-90Ar1 recommendation
HMAC-DRBG is used in an implementation derived from the google/rappor project. HMAC-DRBG seems to be a better choice than the also recommended HASH-DRBG approach. Java 9 is expected to have it's own provider for it. There is no known issue with Java's current SHA1-PRNG implementation, but it is less studied thant the NIST recommendation.
This implementation uses HMAC-SHA512 internally and reseeds itself after 1 MiB of random data generation which is well below the maximum NIST recommendation.
The reports can be seen in
/misc/reports/* in this repo.
A DRGB needs to be seeded by strong entropy sources so it can safely be expanded to create unpredictable pseudo random output. SP800-90Ar1 defines different types of input for the DRGB. This implementation uses the following types:
This implementation uses multiple entropy sources to seed it's random bit generator. All these sources are combined and a weak source will not weaken the overall output. This ensures that even if one source fails the output is still cryptographically strong. Below is a detailed description of the used sources:
This is the main entropy source. This implementation uses the
SecureRandom class with
getStrongInstance() constructor to get the best cryptographic random generator available. Internally
SecureRandom chooses among providers available at runtime. The best of those access the OS own entropy pools (e.g.
/dev/random in *nix systems) since the OS has better access to various random sources.
Per default the tool tries to fetch a seed from an external (supposedly true) random source.
Because there are various opinions what technique delivers truly random data, this tool incorporates 3 different services backed by different hardware RNG. Also to mitigate the fact that if one ore more source is either compromised or produces predictable outcome, the other source will mitigate that flaw.
Using an external random might open a new attack vector if, for example, an attacker might read the seed send over the network. There are 2 measures against this:
Random.org is a website that produces "true random numbers" based on atmospheric noise captured by several radios tuned between stations. The service has existed since 1998 and was built by Dr. Mads Haahr of the School of Computer Science and Statistics at Trinity College, Dublin in Ireland. Random.org offers Transport Layer Security (TLS) encrypted access and signed random data with JSON-RPC 2.0
Hotbits is a "genuine random numbers" service generating data by timing successive pairs of radioactive decays detected by a Geiger-Müller tube interfaced to a computer. This service was created by John Walker in 1996. Hotbits offers raw bytes with a simple HTTP GET request over TLS.
A quantum random number generator offered by the Australian National University. The random numbers are generated in real-time by measuring the quantum fluctuations of the vacuum. The services provides a TLS encrypted JSON/REST API.
The caller may provide a string that additionally seeds the random bit generator. A seed provided by the user is seen as weak seed and will always
be combined with the internal state of a strong
A port of SUN's threaded seed generator by Joshua Bloch. The seed is produced by counting the number of times the VM manages to loop in a given period. This number roughly reflects the machine load at that point in time. The samples are translated using a permutation (s-box) and then XORed together. This process is non linear and should prevent the samples from "averaging out". This implementation uses faster timings to produce values faster.
This generator is mainly used as a fallback if there is no external seed and the internal seeds fail.
The nonce is composed of:
The four 8 byte values will be hashed with HKDF.
The goal of a personalization string is to gather as much information about e.g. runtime, machine identifiers and static identifiers to make the call as unique as possible for this particular machine/runtime/version/etc.
For this the following data will be gathered:
The resulting data will be hashed with HKDF.
misc/example a couple of files of example random data can be found (e.g. 1MiB and 10MiB)
If you want to jar sign you need to provide a file
keystore.jks in the
root folder with the correct credentials set in environment variables (
OPENSOURCE_PROJECTS_KEY_PW); alias is
If you want to skip jar signing just change the skip configuration in the
pom.xml jar sign plugin to true:
Use maven (3.1+) to create a jar including all dependencies
1mvn clean install
This project uses my
common-parent which centralized a lot of
the plugin versions aswell as providing the checkstyle config rules. Specifically they are maintained in
checkstyle-config. Locally the files will be copied after you
mvnw install into your
target folder and is called
target/checkstyle-checker.xml. So if you use a plugin for your IDE, use this file as your local configuration.
Copyright 2017 Patrick Favre-Bulle
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.Star
Overview How to Centralize your Checkstyle Configuration with Maven A Better Way to Protect Your IDs Security Best Practices: Symmetric Encryption with AES in Java and Android: Part 2: AES-CBC + HMAC The Bcrypt Protocol… is kind of a mess The Concise Interface Implementation Pattern Improving ProGuard Name Obfuscation Handling Proguard as Library Developer Managing Logging in a Multi-Module Android App Security Best Practices: Symmetric Encryption with AES in Java and Android
Patrick Favre-Bulle 2020