RustCrypto: Digest Algorithm Traits
Traits which describe functionality of cryptographic hash functions, a.k.a. digest algorithms.
See RustCrypto/hashes for implementations which use this trait.
Minimum Supported Rust Version
Rust 1.41 or higher.
Minimum supported Rust version can be changed in the future, but it will be done with a minor version bump.
- All on-by-default features of this library are covered by SemVer
- MSRV is considered exempt from SemVer as noted above
Let us demonstrate how to use crates in this repository using Sha256 as an example.
First add the
sha2 crate to your
 = "0.10"
sha2 and other crates re-export
digest crate and
Digest trait for
convenience, so you don't have to add
digest crate as an explicit dependency.
Now you can write the following code:
use ; let mut hasher = new; let data = b"Hello world!"; hasher.update; // `input` can be called repeatedly and is generic over `AsRef<[u8]>` hasher.update; // Note that calling `finalize()` consumes hasher let hash = hasher.finalize; println!;
In this example
hash has type
GenericArray<u8, U64>, which is a generic
Alternatively you can use chained approach, which is equivalent to the previous example:
let hash = new .chain_update .chain_update .finalize; println!;
If the whole message is available you also can use convenience
let hash = digest; println!;
use ; use ; let mut file = open?; let mut hasher = new; let n = copy?; let hash = hasher.finalize; println!; println!; println!;
You can write generic code over
Digest (or other traits from
trait which will work over different hash functions:
use Digest; // Toy example, do not use it in practice! // Instead use crates from: https://github.com/RustCrypto/password-hashing let mut buf1 = ; let mut buf2 = ; ; ;
If you want to use hash functions with trait objects, use
Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.