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
| Function | Description |
|---|---|
crypto.hash(algo, data) → string | Hex digest. algo: "sha256", "sha512", "sha1", "md5". |
crypto.hmac(algo, key, data) → string | Hex HMAC. |
Codecs
| Function | Description |
|---|---|
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
| Function | Description |
|---|---|
crypto.random_bytes(n) → string | n cryptographically secure random bytes. |
crypto.random_hex(n) → string | n random bytes formatted as a 2n-character hex string. |
Comparison
| Function | Description |
|---|---|
crypto.equals(a, b) → bool | Constant-time string compare — use for tokens and HMAC verification. |