Skip to contents

Produce cryptographically secure tokens for magic links, session identifiers, and other authentication flows where unpredictability is critical. Uses OpenSSL's cryptographically secure random number generator. Call this when creating magic link tokens, session IDs, or any security-sensitive random values that must be unguessable.

Usage

generate_secure_token(length = 32)

Arguments

length

Integer. Number of random bytes to generate (default: 32). Valid range: 16 to 128 bytes. The returned hex string will be 2x this length in characters. Use 32 bytes (64 hex chars) for standard security, 64 bytes (128 hex chars) for high-security contexts.

Value

Character string containing the hex-encoded token (2x length chars). For example, length=32 returns a 64-character hex string suitable for magic links or session tokens.

See also

Examples

if (FALSE) { # \dontrun{
# Generate a token for a magic link
token <- generate_secure_token(32)
print(nchar(token))  # 64 hex characters

# Store the hashed version in the database
hashed <- hash_token(token)
} # }