Skip to content

Commit feea49e

Browse files
authored
update README examples (#173)
1 parent fcb7516 commit feea49e

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

README.md

+23-17
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ First add `blake2` crate to your `Cargo.toml`:
7070

7171
```toml
7272
[dependencies]
73-
blake2 = "0.8"
73+
blake2 = "0.9"
7474
```
7575

7676
Note that crates in this repository have an enabled by default `std` feature.
7777
So if you plan to use the crate in `no_std` enviroments, don't forget to disable it:
7878

7979
```toml
8080
[dependencies]
81-
blake2 = { version="0.8", default-features = false }
81+
blake2 = { version="0.9", default-features = false }
8282
```
8383

8484
`blake2` and other crates re-export `digest` crate and `Digest` trait for
@@ -91,11 +91,11 @@ use blake2::{Blake2b, Digest};
9191

9292
let mut hasher = Blake2b::new();
9393
let data = b"Hello world!";
94-
hasher.input(data);
95-
// `input` can be called repeatedly and is generic over `AsRef<[u8]>`
96-
hasher.input("String data");
94+
hasher.update(data);
95+
// `update` can be called repeatedly and is generic over `AsRef<[u8]>`
96+
hasher.update("String data");
9797
// Note that calling `result()` consumes hasher
98-
let hash = hasher.result();
98+
let hash = hasher.finalize();
9999
println!("Result: {:x}", hash);
100100
```
101101

@@ -106,24 +106,28 @@ Alternatively you can use chained approach, which is equivalent to the previous
106106
example:
107107

108108
```Rust
109+
use blake2::{Blake2b, Digest};
110+
109111
let hash = Blake2b::new()
110112
.chain(b"Hello world!")
111113
.chain("String data")
112-
.result();
114+
.finalize();
113115
println!("Result: {:x}", hash);
114116
```
115117

116118
If the whole message is available you also can use convinience `digest` method:
117119

118120
```Rust
121+
use blake2::{Blake2b, Digest};
122+
119123
let hash = Blake2b::digest(b"my message");
120124
println!("Result: {:x}", hash);
121125
```
122126

123127
### Hashing `Read`able objects
124128

125129
If you want to hash data from [`Read`][3] trait (e.g. from file) you can rely on
126-
implementation of [`Write`][4] trait (requires enabled-by-default `std` feature):
130+
implementation of [`Write`][4] trait (requires an enabled-by-default `std` feature):
127131

128132
```Rust
129133
use blake2::{Blake2b, Digest};
@@ -132,7 +136,7 @@ use std::{fs, io};
132136
let mut file = fs::File::open(&path)?;
133137
let mut hasher = Blake2b::new();
134138
let n = io::copy(&mut file, &mut hasher)?;
135-
let hash = hasher.result();
139+
let hash = hasher.finalize();
136140
println!("Path: {}", path);
137141
println!("Bytes processed: {}", n);
138142
println!("Hash value: {:x}", hash);
@@ -151,22 +155,24 @@ trait which will work over different hash functions:
151155

152156
```Rust
153157
use digest::Digest;
158+
use blake2::Blake2b;
159+
use sha2::Sha256;
154160

155161
// Toy example, do not use it in practice!
156162
// Instead use crates from: https://github.com/RustCrypto/password-hashing
157163
fn hash_password<D: Digest>(password: &str, salt: &str, output: &mut [u8]) {
158164
let mut hasher = D::new();
159-
hasher.input(password.as_bytes());
160-
hasher.input(b"$");
161-
hasher.input(salt.as_bytes());
162-
output.copy_from_slice(hasher.result().as_slice())
165+
hasher.update(password.as_bytes());
166+
hasher.update(b"$");
167+
hasher.update(salt.as_bytes());
168+
output.copy_from_slice(&hasher.finalize())
163169
}
164170

165-
use blake2::Blake2b;
166-
use sha2::Sha256;
171+
let mut buf1 = [0u8; 64];
172+
hash_password::<Blake2b>("my_password", "abcd", &mut buf1);
167173

168-
hash_password::<Blake2b>("my_password", "abcd", &mut buf);
169-
hash_password::<Sha256>("my_password", "abcd", &mut buf);
174+
let mut buf2 = [0u8; 32];
175+
hash_password::<Sha256>("my_password", "abcd", &mut buf2);
170176
```
171177

172178
If you want to use hash functions with trait objects, use `digest::DynDigest`

0 commit comments

Comments
 (0)