SecureRandom

The SecureRandom filter maps Ruby’s SecureRandom class to the Web Crypto API, which is available in all modern browsers and Node.js. No npm dependencies required.

List of Transformations

  • SecureRandom.uuid crypto.randomUUID()
  • SecureRandom.alphanumeric(n) helper using crypto.getRandomValues
  • SecureRandom.hex(n) helper using crypto.getRandomValues
  • SecureRandom.random_number helper using crypto.getRandomValues
  • SecureRandom.random_number(n) helper using crypto.getRandomValues
  • SecureRandom.base64(n) helper using crypto.getRandomValues

Default length is 16 when not specified (matching Ruby’s defaults).

Examples

# Generate a UUID
token = SecureRandom.uuid
let token = crypto.randomUUID()
# Generate a random alphanumeric string
code = SecureRandom.alphanumeric(12)
let code = _secureRandomAlphanumeric(12)
# Generate a random hex string
key = SecureRandom.hex(32)
let key = _secureRandomHex(32)

Helper functions are automatically prepended to the file when needed. Each helper is only included once, even if multiple SecureRandom calls are made.