#message-authentication #digest-algorithm #hash

no-std digest

Traits for cryptographic hash functions and message authentication codes

24 releases

0.11.0-pre.9 Jul 26, 2024
0.11.0-pre.8 Feb 1, 2024
0.11.0-pre.7 Jan 17, 2024
0.11.0-pre Dec 19, 2022
0.3.0 Nov 17, 2016

#75 in Cryptography

Download history 2206715/week @ 2024-07-15 2305261/week @ 2024-07-22 2301038/week @ 2024-07-29 2319938/week @ 2024-08-05 2419000/week @ 2024-08-12 2375529/week @ 2024-08-19 2410146/week @ 2024-08-26 2340738/week @ 2024-09-02 2371184/week @ 2024-09-09 2422196/week @ 2024-09-16 2517661/week @ 2024-09-23 3186462/week @ 2024-09-30 3849503/week @ 2024-10-07 3640105/week @ 2024-10-14 2831450/week @ 2024-10-21 2532640/week @ 2024-10-28

13,205,710 downloads per month
Used in 24,893 crates (791 directly)

MIT/Apache

110KB
2.5K SLoC

RustCrypto: Digest Algorithm Traits

crate Docs Apache2/MIT licensed Rust Version Project Chat Build Status

Traits which describe functionality of cryptographic hash functions, a.k.a. digest algorithms.

See RustCrypto/hashes for implementations which use this trait.

Documentation

Minimum Supported Rust Version

Rust 1.71 or higher.

Minimum supported Rust version can be changed in the future, but it will be done with a minor version bump.

SemVer Policy

  • All on-by-default features of this library are covered by SemVer
  • MSRV is considered exempt from SemVer as noted above

Usage

Let us demonstrate how to use crates in this repository using Sha256 as an example.

First add the sha2 crate to your Cargo.toml:

[dependencies]
sha2 = "0.11"

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 sha2::{Sha256, Digest};

let mut hasher = Sha256::new();
let data = b"Hello world!";
hasher.update(data);
// `input` can be called repeatedly and is generic over `AsRef<[u8]>`
hasher.update("String data");
// Note that calling `finalize()` consumes hasher
let hash = hasher.finalize();
println!("Result: {:x}", hash);

In this example hash has type Array<u8, U64>, which is a generic alternative to [u8; 64].

Alternatively you can use chained approach, which is equivalent to the previous example:

let hash = Sha256::new()
    .chain_update(b"Hello world!")
    .chain_update("String data")
    .finalize();

println!("Result: {:x}", hash);

If the whole message is available you also can use convenience digest method:

let hash = Sha256::digest(b"my message");
println!("Result: {:x}", hash);

Hashing Read-able objects

If you want to hash data from Read trait (e.g. from file) you can rely on implementation of Write trait (requires enabled-by-default std feature):

use sha2::{Sha256, Digest};
use std::{fs, io};

let mut file = fs::File::open(&path)?;
let mut hasher = Sha256::new();
let n = io::copy(&mut file, &mut hasher)?;
let hash = hasher.finalize();

println!("Path: {}", path);
println!("Bytes processed: {}", n);
println!("Hash value: {:x}", hash);

Generic code

You can write generic code over Digest (or other traits from digest crate) trait which will work over different hash functions:

use digest::Digest;

// Toy example, do not use it in practice!
// Instead use crates from: https://github.com/RustCrypto/password-hashing
fn hash_password<D: Digest>(password: &str, salt: &str, output: &mut [u8]) {
    let mut hasher = D::new();
    hasher.update(password.as_bytes());
    hasher.update(b"$");
    hasher.update(salt.as_bytes());
    output.copy_from_slice(hasher.finalize().as_slice())
}

let mut buf1 = [0u8; 32];
let mut buf2 = [0u8; 64];

hash_password::<sha2::Sha256>("my_password", "abcd", &mut buf1);
hash_password::<sha2::Sha512>("my_password", "abcd", &mut buf2);

If you want to use hash functions with trait objects, use digest::DynDigest trait.

License

Licensed under either of:

at your option.

Contribution

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.

Dependencies

~195–460KB
~10K SLoC