• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-818489

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D10-Jun-202074 65

CHANGELOG.mdH A D10-Jun-20202 KiB7952

Cargo.tomlH A D10-Jun-20201.1 KiB3935

Cargo.toml.orig-cargoH A D10-Jun-2020601 2622

LICENSE-APACHEH A D28-Aug-201910.6 KiB202169

LICENSE-MITH A D10-May-20201 KiB2622

README.mdH A D03-Jun-20204.5 KiB162116

README.md

1# RustCrypto: Digest Algorithm Traits
2
3[![crate][crate-image]][crate-link]
4[![Docs][docs-image]][docs-link]
5![Apache2/MIT licensed][license-image]
6![Rust Version][rustc-image]
7[![Build Status][build-image]][build-link]
8
9Traits which describe functionality of [cryptographic hash functions][0], a.k.a.
10digest algorithms.
11
12See [RustCrypto/hashes][1] for implementations which use this trait.
13
14[Documentation][docs-link]
15
16## Minimum Supported Rust Version
17
18Rust **1.41** or higher.
19
20Minimum supported Rust version can be changed in the future, but it will be
21done with a minor version bump.
22
23## SemVer Policy
24
25- All on-by-default features of this library are covered by SemVer
26- MSRV is considered exempt from SemVer as noted above
27
28## Usage
29
30Let us demonstrate how to use crates in this repository using BLAKE2b as an
31example.
32
33First add `blake2` crate to your `Cargo.toml`:
34
35```toml
36[dependencies]
37blake2 = "0.8"
38```
39
40`blake2` and other crates re-export `digest` crate and `Digest` trait for
41convenience, so you don't have to add `digest` crate as an explicit dependency.
42
43Now you can write the following code:
44
45```rust
46use blake2::{Blake2b, Digest};
47
48let mut hasher = Blake2b::new();
49let data = b"Hello world!";
50hasher.input(data);
51// `input` can be called repeatedly and is generic over `AsRef<[u8]>`
52hasher.input("String data");
53// Note that calling `finalize()` consumes hasher
54let hash = hasher.finalize();
55println!("Result: {:x}", hash);
56```
57
58In this example `hash` has type [`GenericArray<u8, U64>`][2], which is a generic
59alternative to `[u8; 64]`.
60
61Alternatively you can use chained approach, which is equivalent to the previous
62example:
63
64```rust
65let hash = Blake2b::new()
66    .chain(b"Hello world!")
67    .chain("String data")
68    .finalize();
69
70println!("Result: {:x}", hash);
71```
72
73If the whole message is available you also can use convinience `digest` method:
74
75```rust
76let hash = Blake2b::digest(b"my message");
77println!("Result: {:x}", hash);
78```
79
80### Hashing `Read`-able objects
81
82If you want to hash data from [`Read`][3] trait (e.g. from file) you can rely on
83implementation of [`Write`][4] trait (requires enabled-by-default `std` feature):
84
85```rust
86use blake2::{Blake2b, Digest};
87use std::{fs, io};
88
89let mut file = fs::File::open(&path)?;
90let mut hasher = Blake2b::new();
91let n = io::copy(&mut file, &mut hasher)?;
92let hash = hasher.finalize();
93
94println!("Path: {}", path);
95println!("Bytes processed: {}", n);
96println!("Hash value: {:x}", hash);
97```
98
99### Generic code
100
101You can write generic code over `Digest` (or other traits from `digest` crate)
102trait which will work over different hash functions:
103
104```rust
105use digest::Digest;
106
107// Toy example, do not use it in practice!
108// Instead use crates from: https://github.com/RustCrypto/password-hashing
109fn hash_password<D: Digest>(password: &str, salt: &str, output: &mut [u8]) {
110    let mut hasher = D::new();
111    hasher.input(password.as_bytes());
112    hasher.input(b"$");
113    hasher.input(salt.as_bytes());
114    output.copy_from_slice(hasher.finalize().as_slice())
115}
116
117use blake2::Blake2b;
118use sha2::Sha256;
119
120hash_password::<Blake2b>("my_password", "abcd", &mut buf);
121hash_password::<Sha256>("my_password", "abcd", &mut buf);
122```
123
124If you want to use hash functions with trait objects, use `digest::DynDigest`
125trait.
126
127## License
128
129Licensed under either of:
130
131 * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
132 * [MIT license](http://opensource.org/licenses/MIT)
133
134at your option.
135
136### Contribution
137
138Unless you explicitly state otherwise, any contribution intentionally submitted
139for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
140dual licensed as above, without any additional terms or conditions.
141
142[//]: # (badges)
143
144[crate-image]: https://img.shields.io/crates/v/digest.svg
145[crate-link]: https://crates.io/crates/digest
146[docs-image]: https://docs.rs/digest/badge.svg
147[docs-link]: https://docs.rs/digest/
148[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
149[rustc-image]: https://img.shields.io/badge/rustc-1.41+-blue.svg
150[build-image]: https://github.com/RustCrypto/traits/workflows/digest/badge.svg?branch=master&event=push
151[build-link]: https://github.com/RustCrypto/traits/actions?query=workflow%3Adigest
152
153[//]: # (general links)
154
155[0]: https://en.wikipedia.org/wiki/Cryptographic_hash_function
156[1]: https://github.com/RustCrypto/hashes
157[2]: https://docs.rs/generic-array
158[3]: https://doc.rust-lang.org/std/io/trait.Read.html
159[4]: https://doc.rust-lang.org/std/io/trait.Write.html
160[5]: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
161[6]: https://github.com/RustCrypto/MACs
162