Import std.crypto.random
std.crypto.random
Random bytes and numbers from the OS, for secrets, and comparing secrets in constant time.
SecureRandom, bytes and numbers from the OS's randomness, through mbedTLS's generator, for keys, salts, nonces, and tokens; and ConstantTimeEquals, which compares secrets without telling where they differ.
std/crypto/random.fb
Types
SecureRandom
struct
Public Struct SecureRandom
Random bytes and numbers from the OS's randomness, for keys, tokens, and other secrets.
Examples
Local token = SecureRandom.Bytes(16)
Expect(token.Count == 16)
Local roll = SecureRandom.Below(6)
Expect(roll >= 0 && roll < 6)-
countrandom bytes. Panics whencountis negative. -
A random number from 0 up to
limit, withoutlimit, each as likely as another. Panics unlesslimitis above 0.
Functions
ConstantTimeEquals
function
Public Function ConstantTimeEquals(a: UInt8[], b: UInt8[]): Bool
Whether a and b have the same bytes, in a time that depends only on their counts, not on
where they first differ, so comparing a secret, like a MAC, tells nothing about it.
Examples
Local secret = SecureRandom.Bytes(32)
Expect(ConstantTimeEquals(secret, secret.Clone()))
Expect(!ConstantTimeEquals(secret, SecureRandom.Bytes(32)))Report an issue with this page or with FutureBlitz on GitHub.