dreamworkAPI
Ctrl K
GitHub
libraryDocs / crypto / random

crypto.random

librarysharedmenusource

Cryptographically secure random generator, use it for secrets: session tokens, keys, nonces, salts.

Output comes from ChaCha20 with fast key erasure (the key is replaced after every refill), so a leaked state does not reveal earlier output.

The key is seeded from the best available source, see crypto.random.source:

  • "os" - the operating system generator (RtlGenRandom on Windows, /dev/urandom elsewhere), needs ffi, the generator is reseeded from it on every refill.
  • "jitter" - without ffi the seed is SHA-256 of high-resolution timer jitter, memory addresses and clocks, and timer samples from every tick keep being mixed in. Strong in practice, but not provable, feed extra entropy with crypto.random.mix when you have some.

Roughly 10 (FFI) to 30 (pure Lua) times slower than math.random per integer, keep math.random for gameplay and identifiers.

Fields

NameTypeDescription
source"os" | "jitter"The source the generator is seeded from.

Functions

crypto.random.bytesReturns the given number of cryptographically secure random bytes.
crypto.random.floatReturns a cryptographically secure random float in the range [0, 1) with full 53-bit precision.
crypto.random.integerReturns a cryptographically secure random integer in the range [min, max], without modulo bias.
crypto.random.mixMixes extra entropy into the generator, it can only make the output stronger.

#crypto.random.bytes

sharedmenunodiscardsource
function crypto.random.bytes(length: integer): string

Returns the given number of cryptographically secure random bytes.

lua
local session_key = crypto.random.bytes( 32 )
local token = base16.encode( crypto.random.bytes( 16 ) ) -- 32 hex digits, 128 bits
Parameters
NameTypeDescription
lengthintegerThe number of bytes, a non-negative integer.
Returns
TypeNameDescription
stringbytesThe random bytes.

#crypto.random.float

sharedmenunodiscardsource
function crypto.random.float(): number

Returns a cryptographically secure random float in the range [0, 1) with full 53-bit precision.

Returns
TypeNameDescription
numbervalueThe random float.

#crypto.random.integer

sharedmenunodiscardsource
function crypto.random.integer(min: integer, max: integer): integer

Returns a cryptographically secure random integer in the range [min, max], without modulo bias.

lua
crypto.random.integer( 0, 9 )      -- one digit of a PIN
crypto.random.integer( 1, 2 ^ 48 ) -- largest supported range
Parameters
NameTypeDescription
minintegerThe smallest value.
maxintegerThe largest value, at most 2^48 above min.
Returns
TypeNameDescription
integervalueThe random integer.

#crypto.random.mix

sharedmenusource
function crypto.random.mix(data: string | number)

Mixes extra entropy into the generator, it can only make the output stronger.

Good inputs are things an attacker cannot see: timings of player input, arrival times of net messages, secrets from a config file.

Parameters
NameTypeDescription
datastring | numberThe entropy to mix in.
Generated from the dreamwork meta branch.