#digest #crypto #hash

no-std digest

Traits for cryptographic hash functions

28 releases

✓ Uses Rust 2018 edition

0.9.0 Jun 10, 2020
0.8.1 Jun 30, 2019
0.8.0 Oct 1, 2018
0.7.5 Jul 13, 2018
0.3.0 Nov 17, 2016

#2 in Cryptography

Download history 103670/week @ 2020-03-16 112941/week @ 2020-03-23 115264/week @ 2020-03-30 113634/week @ 2020-04-06 121956/week @ 2020-04-13 128697/week @ 2020-04-20 132183/week @ 2020-04-27 128713/week @ 2020-05-04 136818/week @ 2020-05-11 128223/week @ 2020-05-18 130856/week @ 2020-05-25 151020/week @ 2020-06-01 164192/week @ 2020-06-08 163761/week @ 2020-06-15 173691/week @ 2020-06-22 156331/week @ 2020-06-29

530,306 downloads per month
Used in 3,350 crates (209 directly)

MIT/Apache

31KB
489 lines

RustCrypto: Digest Algorithm Traits

crate Docs Apache2/MIT licensed Rust Version 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.41 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 BLAKE2b as an example.

First add blake2 crate to your Cargo.toml:

[dependencies]
blake2 = "0.8"

blake2 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 blake2::{Blake2b, Digest};

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

In this example hash has type GenericArray<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 = Blake2b::new()
    .chain(b"Hello world!")
    .chain("String data")
    .finalize();

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

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

let hash = Blake2b::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 blake2::{Blake2b, Digest};
use std::{fs, io};

let mut file = fs::File::open(&path)?;
let mut hasher = Blake2b::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.input(password.as_bytes());
    hasher.input(b"$");
    hasher.input(salt.as_bytes());
    output.copy_from_slice(hasher.finalize().as_slice())
}

use blake2::Blake2b;
use sha2::Sha256;

hash_password::<Blake2b>("my_password", "abcd", &mut buf);
hash_password::<Sha256>("my_password", "abcd", &mut buf);

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

~235KB