si-crypto-hashes 0.1.2

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

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

# use std::str::FromStr;
# use std::sync::Arc;
# use si_crypto_hashes::{HashAlgorithm, HashDigest};
#
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.