Debian Codegraph @

debian/rust-vendor/digest/README.md

raw
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 ## Usage
16
17 Let us demonstrate how to use crates in this repository using Sha256 as an
18 example.
19
20 First add the `sha2` crate to your `Cargo.toml`:
21
22 ```toml
23 [dependencies]
24 sha2 = "0.11"
25 ```
26
27 `sha2` and other crates re-export `digest` crate and `Digest` trait for
28 convenience, so you don't have to add `digest` crate as an explicit dependency.
29
30 Now you can write the following code:
31
32 ```rust
33 use sha2::{Sha256, Digest};
34
35 let mut hasher = Sha256::new();
36 let data = b"Hello world!";
37 hasher.update(data);
38 // `input` can be called repeatedly and is generic over `AsRef<[u8]>`
39 hasher.update("String data");
40 // Note that calling `finalize()` consumes hasher
41 let hash = hasher.finalize();
42 println!("Result: {:?}", hash);
43 ```
44
45 In this example `hash` has type [`Array<u8, U32>`][2], which is a generic
46 alternative to `[u8; 32]`.
47
48 Alternatively you can use chained approach, which is equivalent to the previous
49 example:
50
51 ```rust
52 use sha2::{Sha256, Digest};
53
54 let hash = Sha256::new()
55 .chain_update(b"Hello world!")
56 .chain_update("String data")
57 .finalize();
58
59 println!("Result: {:?}", hash);
60 ```
61
62 If the whole message is available you also can use convenience `digest` method:
63
64 ```rust
65 use sha2::{Sha256, Digest};
66
67 let hash = Sha256::digest(b"my message");
68 println!("Result: {:?}", hash);
69 ```
70
71 ### Generic code
72
73 You can write generic code over `Digest` (or other traits from `digest` crate)
74 trait which will work over different hash functions:
75
76 ```rust
77 use digest::Digest;
78
79 // Toy example, do not use it in practice!
80 // Instead use crates from: https://github.com/RustCrypto/password-hashing
81 fn hash_password<D: Digest>(password: &str, salt: &str, output: &mut [u8]) {
82 let mut hasher = D::new();
83 hasher.update(password.as_bytes());
84 hasher.update(b"$");
85 hasher.update(salt.as_bytes());
86 output.copy_from_slice(hasher.finalize().as_slice())
87 }
88
89 let mut buf1 = [0u8; 32];
90 let mut buf2 = [0u8; 64];
91
92 hash_password::<sha2::Sha256>("my_password", "abcd", &mut buf1);
93 hash_password::<sha2::Sha512>("my_password", "abcd", &mut buf2);
94 ```
95
96 If you want to use hash functions with trait objects, use `digest::DynDigest`
97 trait.
98
99 ## License
100
101 Licensed under either of:
102
103 * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
104 * [MIT license](http://opensource.org/licenses/MIT)
105
106 at your option.
107
108 ### Contribution
109
110 Unless you explicitly state otherwise, any contribution intentionally submitted
111 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
112 dual licensed as above, without any additional terms or conditions.
113
114 [//]: # (badges)
115
116 [crate-image]: https://img.shields.io/crates/v/digest.svg
117 [crate-link]: https://crates.io/crates/digest
118 [docs-image]: https://docs.rs/digest/badge.svg
119 [docs-link]: https://docs.rs/digest/
120 [license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
121 [rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
122 [chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
123 [chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260041-hashes
124 [build-image]: https://github.com/RustCrypto/traits/actions/workflows/digest.yml/badge.svg?branch=master
125 [build-link]: https://github.com/RustCrypto/traits/actions/workflows/digest.yml?query=branch:master
126
127 [//]: # (general links)
128
129 [0]: https://en.wikipedia.org/wiki/Cryptographic_hash_function
130 [1]: https://github.com/RustCrypto/hashes
131 [2]: https://docs.rs/hybrid-array
132 [3]: https://doc.rust-lang.org/std/io/trait.Read.html
133 [4]: https://doc.rust-lang.org/std/io/trait.Write.html
134 [5]: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
135 [6]: https://github.com/RustCrypto/MACs