Inherited from v1.0.0

crypto module

Cryptographic primitives backed by OpenSSL — hashes, HMAC, base64 (standard and URL-safe), hex, secure random, and constant-time comparison.

import "crypto" as crypto;

print(crypto.hash("sha256", "hello"));               // hex digest
print(crypto.hmac("sha256", "secret", "msg"));       // hex
print(crypto.b64_encode("Hello!"));                  // SGVsbG8h
var token = crypto.random_hex(16);                   // 32-char hex token

Hashing

FunctionDescription
crypto.hash(algo, data) → stringHex digest. algo: "sha256", "sha512", "sha1", "md5".
crypto.hmac(algo, key, data) → stringHex HMAC.

Codecs

FunctionDescription
crypto.b64_encode(data) / b64_decode(s)Standard base64.
crypto.b64url_encode(data) / b64url_decode(s)URL-safe base64 (no padding).
crypto.hex_encode(data) / hex_decode(s)Bytes ↔ hex string.

Random

FunctionDescription
crypto.random_bytes(n) → stringn cryptographically secure random bytes.
crypto.random_hex(n) → stringn random bytes formatted as a 2n-character hex string.

Comparison

FunctionDescription
crypto.equals(a, b) → boolConstant-time string compare — use for tokens and HMAC verification.

See also

  • uuid — when you want a UUID rather than raw random bytes.
  • random — non-cryptographic RNG for tests/games.