Skip to content

crypto

let crypto = import("crypto")
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.

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))

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)