crypto.random
Get cryptographically secure random bytes.
📝Syntax
bytes = crypto.random(n)
hexa = crypto.random(n, '-hex')
📥Input Arguments
Parameter Description
n an integer between 1 and 1048576: number of random bytes.
'-hex' return the bytes as a hexadecimal character vector instead of uint8.
📤Output Arguments
Parameter Description
bytes a uint8 row vector of n secure random bytes.
hexa a character vector: 2 * n lowercase hexadecimal characters.
📄Description

crypto.random returns random bytes from the operating system secure random source (BCryptGenRandom on Windows, /dev/urandom on other systems). Use it for keys, nonces and salts. Unlike rand, its output is unpredictable and must not be seeded.

💡Examples
key = crypto.random(32)
nonce = crypto.random(24, '-hex')
🔗See Also
crypto.aead.encryptcrypto.x25519.keypair
🕔Version History
Version Description
2.0.0 initial version
Edit this page on GitHub