TCryptoRandom
Generate unpredictable data, suitable for creating secret keys.
If this is used in an application inside a VM, and the VM is snapshotted and restored, then the above functions will produce the same output.
Functions
Function Random:UInt()
Returns an unpredictable value between 0 and $ffffffff (inclusive).
Example
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
For Local i:Int = 0 Until 10
Print TCryptoRandom.Random()
Next
Function Uniform:UInt(upperBound:UInt)
Returns an unpredictable value between 0 and upperBound (excluded).
Unlike Random() Mod upperBound
, it does its best to guarantee a uniform distribution of the possible
output values even when upperBound is not a power of 2.
Example
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
For Local i:Int = 0 Until 10
Print TCryptoRandom.Uniform(1000)
Next
Function FillBuffer(buf:Byte Ptr, size:Size_T)
Fills size bytes starting at buf with an unpredictable sequence of bytes, derived from a secret seed.
Example
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
Local buf:Byte[32]
TCryptoRandom.FillBuffer(buf)
For Local i:Int = 0 Until buf.length
Print buf[i]
Next
Function FillBuffer(buf:Byte[])
Fills an array of bytes with an unpredictable sequence of bytes, derived from a secret seed.
Example
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
Local buf:Byte[32]
TCryptoRandom.FillBuffer(buf)
For Local i:Int = 0 Until buf.length
Print buf[i]
Next
Function FillKey(key:TCryptoKey Var, bytes:Size_T = 32)
Fills a key with an unpredictable sequence of bytes in length, derived from a secret seed.
Function Ratchet()
Erases part of the state and replaces the secret key, making it impossible to recover the previous states in case the current one ever gets exposed due to a vulnerability.
Example
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
For Local i:Int = 0 Until 10
Print TCryptoRandom.Random()
Next
TCryptoRandom.Ratchet()
Function Reseed()
Reseeds the random number generator.
Must be called after a fork()
call.
Example
SuperStrict
Framework brl.standardio
Import Crypto.Crypto
For Local i:Int = 0 Until 10
Print TCryptoRandom.Random()
Next
TCryptoRandom.Reseed()