interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / digest / README.md

164 lines · 4854 bytes Raw Blame History Rendered
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
10Traits which describe functionality of [cryptographic hash functions][0], a.k.a.
11digest algorithms.
12
13See [RustCrypto/hashes][1] for implementations which use this trait.
14
15[Documentation][docs-link]
16
17## Minimum Supported Rust Version
18
19Rust **1.41** or higher.
20
21Minimum supported Rust version can be changed in the future, but it will be
22done 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
31Let us demonstrate how to use crates in this repository using Sha256 as an
32example.
33
34First add the `sha2` crate to your `Cargo.toml`:
35
36```toml
37[dependencies]
38sha2 = "0.10"
39```
40
41`sha2` and other crates re-export `digest` crate and `Digest` trait for
42convenience, so you don't have to add `digest` crate as an explicit dependency.
43
44Now you can write the following code:
45
46```rust
47use sha2::{Sha256, Digest};
48
49let mut hasher = Sha256::new();
50let data = b"Hello world!";
51hasher.update(data);
52// `input` can be called repeatedly and is generic over `AsRef<[u8]>`
53hasher.update("String data");
54// Note that calling `finalize()` consumes hasher
55let hash = hasher.finalize();
56println!("Result: {:x}", hash);
57```
58
59In this example `hash` has type [`GenericArray<u8, U64>`][2], which is a generic
60alternative to `[u8; 64]`.
61
62Alternatively you can use chained approach, which is equivalent to the previous
63example:
64
65```rust
66let hash = Sha256::new()
67 .chain_update(b"Hello world!")
68 .chain_update("String data")
69 .finalize();
70
71println!("Result: {:x}", hash);
72```
73
74If the whole message is available you also can use convenience `digest` method:
75
76```rust
77let hash = Sha256::digest(b"my message");
78println!("Result: {:x}", hash);
79```
80
81### Hashing `Read`-able objects
82
83If you want to hash data from [`Read`][3] trait (e.g. from file) you can rely on
84implementation of [`Write`][4] trait (requires enabled-by-default `std` feature):
85
86```rust
87use sha2::{Sha256, Digest};
88use std::{fs, io};
89
90let mut file = fs::File::open(&path)?;
91let mut hasher = Sha256::new();
92let n = io::copy(&mut file, &mut hasher)?;
93let hash = hasher.finalize();
94
95println!("Path: {}", path);
96println!("Bytes processed: {}", n);
97println!("Hash value: {:x}", hash);
98```
99
100### Generic code
101
102You can write generic code over `Digest` (or other traits from `digest` crate)
103trait which will work over different hash functions:
104
105```rust
106use digest::Digest;
107
108// Toy example, do not use it in practice!
109// Instead use crates from: https://github.com/RustCrypto/password-hashing
110fn 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
118let mut buf1 = [0u8; 32];
119let mut buf2 = [0u8; 64];
120
121hash_password::<sha2::Sha256>("my_password", "abcd", &mut buf1);
122hash_password::<sha2::Sha512>("my_password", "abcd", &mut buf2);
123```
124
125If you want to use hash functions with trait objects, use `digest::DynDigest`
126trait.
127
128## License
129
130Licensed 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
135at your option.
136
137### Contribution
138
139Unless you explicitly state otherwise, any contribution intentionally submitted
140for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
141dual 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