diff --git a/digest/README.md b/digest/README.md index fb0ffa71..e1a0f8d9 100644 --- a/digest/README.md +++ b/digest/README.md @@ -6,10 +6,10 @@ ![Rust Version][rustc-image] [![Build Status][build-image]][build-link] -Traits which describe functionality of [cryptographic hash functions], a.k.a. +Traits which describe functionality of [cryptographic hash functions][0], a.k.a. digest algorithms. -See [RustCrypto/hashes] for implementations which use this trait. +See [RustCrypto/hashes][1] for implementations which use this trait. [Documentation][docs-link] @@ -25,6 +25,105 @@ 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 +## Usage + +Let us demonstrate how to use crates in this repository using BLAKE2b as an +example. + +First add `blake2` crate to your `Cargo.toml`: + +```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: + +```rust +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 `result()` consumes hasher +let hash = hasher.result(); +println!("Result: {:x}", hash); +``` + +In this example `hash` has type [`GenericArray`][2], which is a generic +alternative to `[u8; 64]`. + +Alternatively you can use chained approach, which is equivalent to the previous +example: + +```rust +let hash = Blake2b::new() + .chain(b"Hello world!") + .chain("String data") + .result(); + +println!("Result: {:x}", hash); +``` + +If the whole message is available you also can use convinience `digest` method: + +```rust +let hash = Blake2b::digest(b"my message"); +println!("Result: {:x}", hash); +``` + +### Hashing `Read`-able objects + +If you want to hash data from [`Read`][3] trait (e.g. from file) you can rely on +implementation of [`Write`][4] trait (requires enabled-by-default `std` feature): + +```rust +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.result(); + +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: + +```rust +use digest::Digest; + +// Toy example, do not use it in practice! +// Instead use crates from: https://github.com/RustCrypto/password-hashing +fn hash_password(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.result().as_slice()) +} + +use blake2::Blake2b; +use sha2::Sha256; + +hash_password::("my_password", "abcd", &mut buf); +hash_password::("my_password", "abcd", &mut buf); +``` + +If you want to use hash functions with trait objects, use `digest::DynDigest` +trait. + ## License Licensed under either of: @@ -53,5 +152,10 @@ dual licensed as above, without any additional terms or conditions. [//]: # (general links) -[cryptographic hash functions]: https://en.wikipedia.org/wiki/Cryptographic_hash_function -[RustCrypto/hashes]: https://github.com/RustCrypto/hashes +[0]: https://en.wikipedia.org/wiki/Cryptographic_hash_function +[1]: https://github.com/RustCrypto/hashes +[2]: https://docs.rs/generic-array +[3]: https://doc.rust-lang.org/std/io/trait.Read.html +[4]: https://doc.rust-lang.org/std/io/trait.Write.html +[5]: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code +[6]: https://github.com/RustCrypto/MACs