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