Skip to Content
i documentation
Standard library referenceModules

std/crypto

SHA-2 digests and key derivation in ordinary immutable i. Digest inputs are bit strings (SHA padding uses their exact bit length). HMAC and KDF inputs must be whole bytes; errors are values. Hex codecs already live in std/bit_array.

CryptoError

CryptoError = InvalidLength | InvalidIterations | UnalignedInput

sha256

The 256-bit SHA-2 digest. For example sha256(<<"abc":utf8>>).

sha256 = (input : BitArray) -> BitArray {

sha512

The 512-bit SHA-2 digest. For example sha512(<<"abc":utf8>>).

sha512 = (input : BitArray) -> BitArray {

hmac_sha256

RFC 4231 HMAC-SHA-256: hmac_sha256(<<1>>, <<2>>).

hmac_sha256 = (key : BitArray, input : BitArray) -> Result(

hmac_sha512

RFC 4231 HMAC-SHA-512, with arbitrary-length byte keys.

hmac_sha512 = (key : BitArray, input : BitArray) -> Result(

hkdf_sha256

Extracts then expands: hkdf_sha256(key, <<>>, <<>>, 32).

hkdf_sha256 = (

hkdf_sha512

HKDF with SHA-512.

hkdf_sha512 = (

hkdf_extract_sha256

RFC 5869 extract. Empty salt is equivalent to a hash-length zero salt.

hkdf_extract_sha256 = (salt : BitArray, input : BitArray) -> Result(

hkdf_extract_sha512

RFC 5869 extract using SHA-512.

hkdf_extract_sha512 = (salt : BitArray, input : BitArray) -> Result(

hkdf_expand_sha256

Expands a SHA-256 PRK into 0..8160 bytes. A short PRK is an error.

hkdf_expand_sha256 = (prk : BitArray, info : BitArray, length : Int) -> Result(

hkdf_expand_sha512

Expands a SHA-512 PRK into 0..16320 bytes.

hkdf_expand_sha512 = (prk : BitArray, info : BitArray, length : Int) -> Result(

pbkdf2_sha256

PBKDF2-HMAC-SHA-256, positive iteration count and 0..(2^32-1)*32 bytes. pbkdf2_sha256(<<"password":utf8>>, <<"salt":utf8>>, 1, 32).

pbkdf2_sha256 = (

constant_time_equal

Compares every bit for equal-length inputs, without a content-dependent exit. Length is public. This source-level timing property is not a host timing guarantee.

constant_time_equal = (a : BitArray, b : BitArray) -> Bool {

random_bytes

Secure bytes from the existing Entropy service. No pseudo-random substitute.

random_bytes = (length : Int) -> Result(BitArray, CryptoError) {