Volt

crypt.random

Generates cryptographically secure random bytes.

Syntax

crypt.random(length: number) -> string

Parameters

ParameterTypeDescription
lengthnumberNumber of random bytes to generate

Returns

TypeDescription
stringRandom byte string

Description

crypt.random generates cryptographically secure random bytes, suitable for security-sensitive operations.

Example

-- Generate 16 random bytes
local randomBytes = crypt.random(16)
print("Random bytes length:", #randomBytes)

-- Convert to hex for display
local hex = ""
for i = 1, #randomBytes do
    hex = hex .. string.format("%02x", string.byte(randomBytes, i))
end
print("Hex:", hex)

Use Cases

  • Generating session tokens
  • Creating random IVs for encryption
  • Generating salts for password hashing

On this page