kryptos/hash

Types

Supported cryptographic hash algorithms.

pub type HashAlgorithm {
  Blake2b
  Blake2s
  Md5
  Sha1
  Sha256
  Sha384
  Sha512
  Sha512x224
  Sha512x256
  Sha3x224
  Sha3x256
  Sha3x384
  Sha3x512
}

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)

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.

Parameters

  • algorithm: The hash algorithm to get the size for

Returns

The digest size in bytes.

pub fn final(hasher: Hasher) -> BitArray

Finalizes the hash computation and returns the digest.

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

Parameters

  • hasher: The hasher to finalize

Returns

A BitArray containing the computed hash digest.

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.

Parameters

  • algorithm: The hash algorithm to use

Returns

  • Ok(Hasher) - A new hasher ready to accept input data
  • Error(Nil) - If the hash algorithm is not supported by the runtime
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.

Parameters

  • hasher: The hasher to update
  • input: The data to add to the hash

Returns

The updated hasher.

Search Document