Crypto & Bytes
Crypto
Section titled “Crypto”The crypto namespace provides hashing, HMAC, encryption, password hashing, and secure random bytes.
Hashing
Section titled “Hashing”| Function | Description |
|---|---|
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")Random bytes
Section titled “Random bytes”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 tokenAES encryption (requires OpenSSL)
Section titled “AES encryption (requires OpenSSL)”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"Password hashing (requires OpenSSL)
Section titled “Password hashing (requires OpenSSL)”PBKDF2-SHA256 for secure password storage:
// Hash a passwordlet result = crypto.hashPassword("mypassword")print(result.hash) // hex hashprint(result.salt) // hex saltprint(result.iterations) // 100000
// Verify a passwordcrypto.verifyPassword("mypassword", result.hash, result.salt) // truecrypto.verifyPassword("wrong", result.hash, result.salt) // falseCustom iterations: crypto.hashPassword("pass", nil, 200000)
Base64
Section titled “Base64”| Function | Description |
|---|---|
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.
Struct format strings
Section titled “Struct format strings”bytes.pack and bytes.unpack accept Python-style struct format strings.
Endian prefix (required):
| Prefix | Byte order |
|---|---|
> or ! | Big-endian (network) |
< or = | Little-endian |
Type characters:
| Char | Size | Description |
|---|---|---|
B | 1 | Unsigned 8-bit |
b | 1 | Signed 8-bit |
H | 2 | Unsigned 16-bit |
h | 2 | Signed 16-bit |
I | 4 | Unsigned 32-bit |
i | 4 | Signed 32-bit |
Q | 8 | Unsigned 64-bit |
q | 8 | Signed 64-bit |
f | 4 | 32-bit float |
d | 8 | 64-bit double |
x | 1 | Pad byte (no value consumed) |
Repeat counts: 3B means three unsigned bytes, 4x means four pad bytes.
bytes.pack / bytes.unpack
Section titled “bytes.pack / bytes.unpack”// Pack big-endian u8 + u16 + u32let data = bytes.pack(">BHI", [255, 1234, 100000])let vals = bytes.unpack(">BHI", data) // [255, 1234, 100000]bytes.calcsize
Section titled “bytes.calcsize”bytes.calcsize(">BHI") // 7bytes.calcsize(">3B2Hd") // 15Byte conversion
Section titled “Byte conversion”let raw = bytes.from([72, 101, 108, 108, 111]) // "Hello"let arr = bytes.toArray("Hello") // [72, 101, 108, 108, 111]
// Hex encodingbytes.hex("ABC") // "414243"bytes.fromHex("414243") // "ABC"
// Byte lengthbytes.len(data)Character codes
Section titled “Character codes”"A".charCode() // 65"hello".charCode(1) // 101fromCharCode(65) // "A"fromCharCode(0x1F600) // emoji