crypto
let crypto = import("crypto")Hashing
Section titled “Hashing”| Function | Returns | Description |
|---|---|---|
sha256(data: string | bytes) |
bytes |
SHA-256 digest as Bytes |
sha256_hex(data: string | bytes) |
string |
SHA-256 digest as a hex string |
sha1(data: string | bytes) |
bytes |
SHA-1 digest as Bytes |
sha1_hex(data: string | bytes) |
string |
SHA-1 digest as a hex string |
md5(data: string | bytes) |
bytes |
MD5 digest as Bytes |
md5_hex(data: string | bytes) |
string |
MD5 digest as a hex string |
hmac_sha256(key: string | bytes, data: string | bytes) |
bytes |
HMAC-SHA256 as Bytes |
hmac_sha256_hex(key: string | bytes, data: string | bytes) |
string |
HMAC-SHA256 as a hex string |
data and key accept a string or Bytes.
let crypto = import("crypto")
print(crypto.sha256_hex("hello"))print(crypto.hmac_sha256_hex("secret", "payload"))SHA-1 and MD5 are legacy algorithms. Use SHA-256 for new designs.
Random bytes
Section titled “Random bytes”crypto.random(n) returns n cryptographically random bytes from a CTR-DRBG. n must be between 0 and 65536.
let encoding = import("encoding")let crypto = import("crypto")
let nonce = crypto.random(16)print(encoding.hex_encode(nonce))Key derivation
Section titled “Key derivation”crypto.pbkdf2(password, salt, iterations, length) derives length bytes with PBKDF2-HMAC-SHA256. Iterations must be between 1 and 10,000,000; length must be between 1 and 1024.
let key = crypto.pbkdf2("password", crypto.random(16), 210000, 32)Related
Section titled “Related”- encoding for Base64, hex, and URL coding.
- Bytes & BytesBuilder for binary data.
- http for sending digests and tokens.