SkillAgentSearch skills...

Dice

A cryptographically secure pseudorandom number generator cli tool printing in a wide variety of byte encodings (hex, base64,..) and for many programming languages (c, java, c#, php, etc.) using NIST SP800-90Ar1 HMAC-DRBG. Supports external seeding from various true random services.

Install / Use

/learn @patrickfav/Dice

README

Dice

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.

GitHub release Github Actions Coverage Security Rating Technical Debt

<sup>* depending on the used provider</sup>

logo

Main features:

Example usage generating randoms with 24 byte-length (not char length) and default encoding:

java -jar dice.jar 24

More examples:

java -jar dice.jar 16 --count 100
java -jar dice.jar 16 --encoding "base64"
java -jar dice.jar 16 --encoding "java"
java -jar dice.jar 4096 --encoding "raw" --count 1024 --file "./rnd-4-MiB-outputfile.txt"
java -jar dice.jar 16 --seed "myBadRandomSeed"
java -jar dice.jar 16 --offline
java -jar dice.jar 32 --encoding "base85" --urlencode --padding --crc32

This should run on any Windows, Mac or Linux machine.

Requirements

Download

Grab jar from the latest Release

Using the *.exe Launcher: Launch4J is used to wrap the .jar into a Windows executable. It should automatically download the needed JRE if required.

Demo

asciicast

Use Cases

Creating Nonces, Tokens, Identifiers or Passwords

I'll recommend base32, base36 or base58 because these encodings are typically url-safe. 16 byte usually suffice for globally unique, infeasible to brute force number.

java -jar dice.jar 16 -e "base36"

If you require fixed char-sized output either use hex encoding or other encodings supporting paddings like base32 or base64. For passwords, high-density encodings are recommended like base85.

Creating static byte arrays for your application

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.:

java -jar dice.jar 16 -e "java"

Creating files of entropy

Create a file 4MiB full of raw random bytes with this call:

java -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.

Command Line Interface

   --anuquantum             Enable external, supposed true random generator ANU Quantum; note this service is known
                            to be slow (only when online).
-c,--count <number>         How many randoms should be generated. Automatically chosen if this argument is omitted.
   --crc32                  If this flag is set, 4 bytes of CRC32 checksum will be appended to every random value.
                            If you need to check the integrity of the data.
-d,--debug                  Prints additional info for debugging.
-e,--encoding <string>      Output byte-to-text encoding. Available encodings include:
                            binary, octal, dec, base16, BASE16, base26, base32, base36, base58, base64, base64-url,
                            base85, c, c#, java, go, kotlin, node, js, perl, php, python3, ruby, rust, swift, img,
                            raw, utf8
-f,--file <path>            Prints the random data to given file instead of the command line. Will create the file
                            if it does not exist or append the data if it does.
-h,--help                   Shows this page.
-o,--offline                Skips request to external random generators (random.org) for seeding (use when
                            offline).
-p,--padding                If this flag is set, byte-to-text output will be padded to full byte if needed.
-r,--robot                  If this flag is set, output will be more friendly for scripting (ie. no verbose text,
                            only the randoms 1 per line)
-s,--seed <string|number>   Uses either the 64-bit integer interpretation or the utf-8 byte representation of given
                            parameter to seed the internal random generator. Warns if entropy is low.
-u,--urlencode              Uses 'www-form-urlencoded' encoding scheme, also misleadingly known as URL encoding, on
                            the output strings
-v,--version                Prints application version.

Supported Encodings

Byte-to-Text Encodings

| Name | Example | Efficiency | Padding | Description | |------------|------------------------------------------------------------------|-----------:|:-------:|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | binary | 11101101 10101111 00011110 11111111 11111101 10010100 01001010 | 12.5 % | false | A simple binary representation with '0' and '1' divided into 8 bit groups. | | octal | 1124517677707527755 | 37.5 % | true | The octal numeral system, is the base-8 number system, and uses the digits 0 to 7. | | dec | 20992966904426477 | 41.5 % | true | Decimal positive sign-magnitude representation representation in big-endian byte-order. | | base16 | 4a94fdff1eafed | 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 | 4A94FDFF1EAFED | 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 | FSSLZZFNQZQZ | 58.8 % | true | Base26 uses the twenty-six letters A-Z. | | base32 | JKKP37Y6V7WQ | 62.5 % | true | Base32 uses a 32-character subset of the twenty-six letters A-Z and the digits 2-7. Uses the alphab

Related Skills

View on GitHub
GitHub Stars14
CategoryCustomer
Updated1y ago
Forks6

Languages

Java

Security Score

80/100

Audited on Jan 6, 2025

No findings