Skip to content

Crypto & Bytes

The crypto namespace provides hashing, HMAC, encryption, password hashing, and secure random bytes.

FunctionDescription
crypto.md5(string)MD5 hash (32-char hex)
crypto.sha1(string)SHA-1 hash (40-char hex)
crypto.sha256(string)SHA-256 hash (64-char hex)
crypto.sha512(string)SHA-512 hash (128-char hex)
crypto.md5("hello") // "5d41402abc4b2a76b9719d911017c592"
crypto.sha1("hello") // "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
crypto.sha256("hello") // "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

crypto.hmac(key, message, algorithm) computes a keyed hash. Supported: "sha256", "sha1", "sha512", "md5".

crypto.hmac("secret-key", "message", "sha256")

crypto.randomBytes(count) returns cryptographically secure random bytes as a raw string:

let key = crypto.randomBytes(32) // 32 random bytes (256-bit key)
let iv = crypto.randomBytes(16) // 16 random bytes (128-bit IV)
let token = bytes.hex(crypto.randomBytes(16)) // hex string token

AES-256-CBC symmetric encryption. Key must be 32 bytes, IV must be 16 bytes.

let key = crypto.randomBytes(32)
let iv = crypto.randomBytes(16)
let encrypted = crypto.encrypt("secret data", key, iv)
let decrypted = crypto.decrypt(encrypted, key, iv)
print(decrypted) // "secret data"

PBKDF2-SHA256 for secure password storage:

// Hash a password
let result = crypto.hashPassword("mypassword")
print(result.hash) // hex hash
print(result.salt) // hex salt
print(result.iterations) // 100000
// Verify a password
crypto.verifyPassword("mypassword", result.hash, result.salt) // true
crypto.verifyPassword("wrong", result.hash, result.salt) // false

Custom iterations: crypto.hashPassword("pass", nil, 200000)

FunctionDescription
base64.encode(str)Encode string to standard base64
base64.decode(str)Decode standard base64 to string
base64.encodeURL(str)URL-safe base64 (RFC 4648): -_ instead of +/, no padding
base64.decodeURL(str)Decode URL-safe base64
base64.encode("hello") // "aGVsbG8="
base64.decode("aGVsbG8=") // "hello"
base64.encodeURL("hello") // "aGVsbG8" (no padding)
base64.decodeURL("aGVsbG8") // "hello"

The bytes namespace provides binary data packing and unpacking.

bytes.pack and bytes.unpack accept Python-style struct format strings.

Endian prefix (required):

PrefixByte order
> or !Big-endian (network)
< or =Little-endian

Type characters:

CharSizeDescription
B1Unsigned 8-bit
b1Signed 8-bit
H2Unsigned 16-bit
h2Signed 16-bit
I4Unsigned 32-bit
i4Signed 32-bit
Q8Unsigned 64-bit
q8Signed 64-bit
f432-bit float
d864-bit double
x1Pad byte (no value consumed)

Repeat counts: 3B means three unsigned bytes, 4x means four pad bytes.

// Pack big-endian u8 + u16 + u32
let data = bytes.pack(">BHI", [255, 1234, 100000])
let vals = bytes.unpack(">BHI", data) // [255, 1234, 100000]
bytes.calcsize(">BHI") // 7
bytes.calcsize(">3B2Hd") // 15
let raw = bytes.from([72, 101, 108, 108, 111]) // "Hello"
let arr = bytes.toArray("Hello") // [72, 101, 108, 108, 111]
// Hex encoding
bytes.hex("ABC") // "414243"
bytes.fromHex("414243") // "ABC"
// Byte length
bytes.len(data)
"A".charCode() // 65
"hello".charCode(1) // 101
fromCharCode(65) // "A"
fromCharCode(0x1F600) // emoji