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
-
Blake2bBLAKE2b (512-bit output)
-
Blake2sBLAKE2s (256-bit output)
-
Md5MD5 (128-bit output), cryptographically broken - use only for legacy compatibility.
-
Sha1SHA-1 (160-bit output)
-
Sha256SHA-256 (256-bit output)
-
Sha384SHA-384 (384-bit output)
-
Sha512SHA-512 (512-bit output)
-
Sha512x224SHA-512/224 (224-bit output), truncated SHA-512.
-
Sha512x256SHA-512/256 (256-bit output), truncated SHA-512.
-
Sha3x224SHA3-224 (224-bit output)
-
Sha3x256SHA3-256 (256-bit output)
-
Sha3x384SHA3-384 (384-bit output)
-
Sha3x512SHA3-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_128smart 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_256smart constructor to validate the output length.
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 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.
Parameters
algorithm: The hash algorithm to check
Returns
True if the algorithm is supported, False otherwise.
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) on success, Error(Nil) if the hash algorithm is not
supported by the runtime.
pub fn shake_128(
output_length length: Int,
) -> Result(HashAlgorithm, Nil)
Creates a SHAKE128 hash algorithm with the given output length in bytes.
Parameters
output_length: The desired digest length in bytes (must be > 0)
Returns
Ok(HashAlgorithm) on success, Error(Nil) if the output length is invalid.
pub fn shake_256(
output_length length: Int,
) -> Result(HashAlgorithm, Nil)
Creates a SHAKE256 hash algorithm with the given output length in bytes.
Parameters
output_length: The desired digest length in bytes (must be > 0)
Returns
Ok(HashAlgorithm) on success, Error(Nil) if the output length is invalid.