@@ -70,15 +70,15 @@ First add `blake2` crate to your `Cargo.toml`:
70
70
71
71
``` toml
72
72
[dependencies ]
73
- blake2 = " 0.8 "
73
+ blake2 = " 0.9 "
74
74
```
75
75
76
76
Note that crates in this repository have an enabled by default ` std ` feature.
77
77
So if you plan to use the crate in ` no_std ` enviroments, don't forget to disable it:
78
78
79
79
``` toml
80
80
[dependencies ]
81
- blake2 = { version =" 0.8 " , default-features = false }
81
+ blake2 = { version =" 0.9 " , default-features = false }
82
82
```
83
83
84
84
` blake2 ` and other crates re-export ` digest ` crate and ` Digest ` trait for
@@ -91,11 +91,11 @@ use blake2::{Blake2b, Digest};
91
91
92
92
let mut hasher = Blake2b :: new ();
93
93
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" );
97
97
// Note that calling `result()` consumes hasher
98
- let hash = hasher . result ();
98
+ let hash = hasher . finalize ();
99
99
println! (" Result: {:x}" , hash );
100
100
```
101
101
@@ -106,24 +106,28 @@ Alternatively you can use chained approach, which is equivalent to the previous
106
106
example:
107
107
108
108
``` Rust
109
+ use blake2 :: {Blake2b , Digest };
110
+
109
111
let hash = Blake2b :: new ()
110
112
. chain (b " Hello world!" )
111
113
. chain (" String data" )
112
- . result ();
114
+ . finalize ();
113
115
println! (" Result: {:x}" , hash );
114
116
```
115
117
116
118
If the whole message is available you also can use convinience ` digest ` method:
117
119
118
120
``` Rust
121
+ use blake2 :: {Blake2b , Digest };
122
+
119
123
let hash = Blake2b :: digest (b " my message" );
120
124
println! (" Result: {:x}" , hash );
121
125
```
122
126
123
127
### Hashing ` Read ` able objects
124
128
125
129
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):
127
131
128
132
``` Rust
129
133
use blake2 :: {Blake2b , Digest };
@@ -132,7 +136,7 @@ use std::{fs, io};
132
136
let mut file = fs :: File :: open (& path )? ;
133
137
let mut hasher = Blake2b :: new ();
134
138
let n = io :: copy (& mut file , & mut hasher )? ;
135
- let hash = hasher . result ();
139
+ let hash = hasher . finalize ();
136
140
println! (" Path: {}" , path );
137
141
println! (" Bytes processed: {}" , n );
138
142
println! (" Hash value: {:x}" , hash );
@@ -151,22 +155,24 @@ trait which will work over different hash functions:
151
155
152
156
``` Rust
153
157
use digest :: Digest ;
158
+ use blake2 :: Blake2b ;
159
+ use sha2 :: Sha256 ;
154
160
155
161
// Toy example, do not use it in practice!
156
162
// Instead use crates from: https://github.com/RustCrypto/password-hashing
157
163
fn hash_password <D : Digest >(password : & str , salt : & str , output : & mut [u8 ]) {
158
164
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 ())
163
169
}
164
170
165
- use blake2 :: Blake2b ;
166
- use sha2 :: Sha256 ;
171
+ let mut buf1 = [ 0 u8 ; 64 ] ;
172
+ hash_password :: < Blake2b >( " my_password " , " abcd " , & mut buf1 ) ;
167
173
168
- hash_password :: < Blake2b >( " my_password " , " abcd " , & mut buf ) ;
169
- hash_password :: <Sha256 >(" my_password" , " abcd" , & mut buf );
174
+ let mut buf2 = [ 0 u8 ; 32 ] ;
175
+ hash_password :: <Sha256 >(" my_password" , " abcd" , & mut buf2 );
170
176
```
171
177
172
178
If you want to use hash functions with trait objects, use ` digest::DynDigest `
0 commit comments