kryptos/hash

Cryptographic hash functions.

Hash functions take arbitrary input data and produce a fixed-size digest. Use these for data integrity verification, fingerprinting, and as building blocks for other cryptographic constructs like HMAC.

Example

import kryptos/hash

let assert Ok(h) = hash.new(hash.Sha256)
let digest = h |> hash.update(<<"hello":utf8>>) |> hash.final()

Types

Supported cryptographic hash algorithms.

pub type HashAlgorithm {
  Blake2b
  Blake2s
  Md5
  Sha1
  Sha256
  Sha384
  Sha512
  Sha512x224
  Sha512x256
  Sha3x224
  Sha3x256
  Sha3x384
  Sha3x512
  Shake128(output_length: Int)
  Shake256(output_length: Int)
}

Constructors

  • Blake2b

    BLAKE2b (512-bit output)

  • Blake2s

    BLAKE2s (256-bit output)

  • Md5

    MD5 (128-bit output), cryptographically broken - use only for legacy compatibility.

  • Sha1

    SHA-1 (160-bit output)

  • Sha256

    SHA-256 (256-bit output)

  • Sha384

    SHA-384 (384-bit output)

  • Sha512

    SHA-512 (512-bit output)

  • Sha512x224

    SHA-512/224 (224-bit output), truncated SHA-512.

  • Sha512x256

    SHA-512/256 (256-bit output), truncated SHA-512.

  • Sha3x224

    SHA3-224 (224-bit output)

  • Sha3x256

    SHA3-256 (256-bit output)

  • Sha3x384

    SHA3-384 (384-bit output)

  • Sha3x512

    SHA3-512 (512-bit output)

  • Shake128(output_length: Int)

    SHAKE128 extendable-output function (128-bit security). The output_length parameter specifies the desired digest length in bytes. Prefer using the shake_128 smart constructor to validate the output length.

  • Shake256(output_length: Int)

    SHAKE256 extendable-output function (256-bit security). The output_length parameter specifies the desired digest length in bytes. Prefer using the shake_256 smart constructor to validate the output length.

Represents an in-progress hash computation.

Use new to create a hasher, update to add data, and final to get the digest.

pub type Hasher

Values

pub fn byte_size(algorithm: HashAlgorithm) -> Int

Returns the output size in bytes for a hash algorithm.

pub fn final(hasher: Hasher) -> BitArray

Finalizes the hash computation and returns the digest.

After calling this function, the hasher should not be reused.

pub fn is_supported(algorithm: HashAlgorithm) -> Bool

Checks if a hash algorithm is supported by the current runtime.

Some algorithms may not be available depending on the platform or OpenSSL/crypto library version.

pub fn new(algorithm: HashAlgorithm) -> Result(Hasher, Nil)

Creates a new hasher for incremental hashing.

Use this when you need to hash data in chunks, such as when streaming or when the full input isn’t available at once.

pub fn shake_128(
  output_length length: Int,
) -> Result(HashAlgorithm, Nil)

Creates a SHAKE128 hash algorithm with the given output length in bytes.

The output length must be greater than zero.

pub fn shake_256(
  output_length length: Int,
) -> Result(HashAlgorithm, Nil)

Creates a SHAKE256 hash algorithm with the given output length in bytes.

The output length must be greater than zero.

pub fn update(hasher: Hasher, data: BitArray) -> Hasher

Adds data to an in-progress hash computation.

Can be called multiple times to incrementally hash data.

Search Document