Crate si_crypto_hashes

Crate si_crypto_hashes 

Source
Expand description

This crate provides a reusable functionality for working with typical cryptographic hashes.

let expected = "sha256_dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f";

// Parse the string representation of the expected hash.
let digest = HashDigest::<Arc<[u8]>>::from_str(expected).unwrap();
assert_eq!(digest.algorithm(), HashAlgorithm::Sha256);
assert_eq!(digest.to_string(), expected);

// Compute a digest.
let mut hasher = digest.algorithm().hasher();
hasher.update(b"Hello, World!");
assert_eq!(hasher.finalize(), digest);

For parsing, the string representation is expected to be in the format <algorithm>_<digest> or <algorithm>:<digest>.

The algorithm must be one of the following:

  • sha256
  • sha512_256 or sha512-256
  • sha512

Note that the underscore representation is preferred as it can be selected by double clicking in most applications.

In the future, we may add additional hash algorithms.

§Features

This crate supports the following features:

  • serde: Enable serialization and deserialization support using Serde.
  • legacy: Use legacy formatting and algorithm names for compatibility with older parsers.

Structs§

HashDigest
Hash digest.
Hasher
Hasher for computing hashes.
InvalidAlgorithmError
Invalid hash algorithm error.
InvalidDigestError
Invalid hash digest error.

Enums§

HashAlgorithm
Cryptographic hash algorithms.