@@ -24,46 +24,44 @@ use core::{borrow, fmt, ops, str};
24
24
#[ cfg( feature = "serde" ) ]
25
25
use serde:: { Serialize , Serializer , Deserialize , Deserializer } ;
26
26
27
- use HashEngine as EngineTrait ;
28
- use Hash as HashTrait ;
29
- use Error ;
27
+ use crate :: { Error , Hash , HashEngine } ;
30
28
31
29
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
32
30
#[ derive( Copy , Clone , PartialEq , Eq , Default , PartialOrd , Ord , Hash ) ]
33
31
#[ cfg_attr( feature = "schemars" , derive( schemars:: JsonSchema ) ) ]
34
32
#[ cfg_attr( feature = "schemars" , schemars( transparent) ) ]
35
33
#[ repr( transparent) ]
36
- pub struct Hmac < T : HashTrait > ( T ) ;
34
+ pub struct Hmac < T : Hash > ( T ) ;
37
35
38
- impl < T : HashTrait + str:: FromStr > str:: FromStr for Hmac < T > {
36
+ impl < T : Hash + str:: FromStr > str:: FromStr for Hmac < T > {
39
37
type Err = <T as str:: FromStr >:: Err ;
40
38
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
41
39
Ok ( Hmac ( str:: FromStr :: from_str ( s) ?) )
42
40
}
43
41
}
44
42
45
43
/// Pair of underlying hash midstates which represent the current state of an `HmacEngine`.
46
- pub struct HmacMidState < T : HashTrait > {
44
+ pub struct HmacMidState < T : Hash > {
47
45
/// Midstate of the inner hash engine
48
- pub inner : <T :: Engine as EngineTrait >:: MidState ,
46
+ pub inner : <T :: Engine as HashEngine >:: MidState ,
49
47
/// Midstate of the outer hash engine
50
- pub outer : <T :: Engine as EngineTrait >:: MidState ,
48
+ pub outer : <T :: Engine as HashEngine >:: MidState ,
51
49
}
52
50
53
51
/// Pair of underyling hash engines, used for the inner and outer hash of HMAC.
54
52
#[ derive( Clone ) ]
55
- pub struct HmacEngine < T : HashTrait > {
53
+ pub struct HmacEngine < T : Hash > {
56
54
iengine : T :: Engine ,
57
55
oengine : T :: Engine ,
58
56
}
59
57
60
- impl < T : HashTrait > Default for HmacEngine < T > {
58
+ impl < T : Hash > Default for HmacEngine < T > {
61
59
fn default ( ) -> Self {
62
60
HmacEngine :: new ( & [ ] )
63
61
}
64
62
}
65
63
66
- impl < T : HashTrait > HmacEngine < T > {
64
+ impl < T : Hash > HmacEngine < T > {
67
65
/// Constructs a new keyed HMAC from `key`.
68
66
///
69
67
/// We only support underlying hashes whose block sizes are ≤ 128 bytes.
@@ -77,12 +75,12 @@ impl<T: HashTrait> HmacEngine<T> {
77
75
let mut ipad = [ 0x36u8 ; 128 ] ;
78
76
let mut opad = [ 0x5cu8 ; 128 ] ;
79
77
let mut ret = HmacEngine {
80
- iengine : <T as HashTrait >:: engine ( ) ,
81
- oengine : <T as HashTrait >:: engine ( ) ,
78
+ iengine : <T as Hash >:: engine ( ) ,
79
+ oengine : <T as Hash >:: engine ( ) ,
82
80
} ;
83
81
84
82
if key. len ( ) > T :: Engine :: BLOCK_SIZE {
85
- let hash = <T as HashTrait >:: hash ( key) ;
83
+ let hash = <T as Hash >:: hash ( key) ;
86
84
for ( b_i, b_h) in ipad. iter_mut ( ) . zip ( & hash[ ..] ) {
87
85
* b_i ^= * b_h;
88
86
}
@@ -98,8 +96,8 @@ impl<T: HashTrait> HmacEngine<T> {
98
96
}
99
97
} ;
100
98
101
- EngineTrait :: input ( & mut ret. iengine , & ipad[ ..T :: Engine :: BLOCK_SIZE ] ) ;
102
- EngineTrait :: input ( & mut ret. oengine , & opad[ ..T :: Engine :: BLOCK_SIZE ] ) ;
99
+ HashEngine :: input ( & mut ret. iengine , & ipad[ ..T :: Engine :: BLOCK_SIZE ] ) ;
100
+ HashEngine :: input ( & mut ret. oengine , & opad[ ..T :: Engine :: BLOCK_SIZE ] ) ;
103
101
ret
104
102
}
105
103
@@ -112,7 +110,7 @@ impl<T: HashTrait> HmacEngine<T> {
112
110
}
113
111
}
114
112
115
- impl < T : HashTrait > EngineTrait for HmacEngine < T > {
113
+ impl < T : Hash > HashEngine for HmacEngine < T > {
116
114
type MidState = HmacMidState < T > ;
117
115
118
116
fn midstate ( & self ) -> Self :: MidState {
@@ -133,66 +131,66 @@ impl<T: HashTrait> EngineTrait for HmacEngine<T> {
133
131
}
134
132
}
135
133
136
- impl < T : HashTrait > fmt:: Debug for Hmac < T > {
134
+ impl < T : Hash > fmt:: Debug for Hmac < T > {
137
135
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
138
136
fmt:: Debug :: fmt ( & self . 0 , f)
139
137
}
140
138
}
141
139
142
- impl < T : HashTrait > fmt:: Display for Hmac < T > {
140
+ impl < T : Hash > fmt:: Display for Hmac < T > {
143
141
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
144
142
fmt:: Display :: fmt ( & self . 0 , f)
145
143
}
146
144
}
147
145
148
- impl < T : HashTrait > fmt:: LowerHex for Hmac < T > {
146
+ impl < T : Hash > fmt:: LowerHex for Hmac < T > {
149
147
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
150
148
fmt:: LowerHex :: fmt ( & self . 0 , f)
151
149
}
152
150
}
153
151
154
- impl < T : HashTrait > ops:: Index < usize > for Hmac < T > {
152
+ impl < T : Hash > ops:: Index < usize > for Hmac < T > {
155
153
type Output = u8 ;
156
154
fn index ( & self , index : usize ) -> & u8 {
157
155
& self . 0 [ index]
158
156
}
159
157
}
160
158
161
- impl < T : HashTrait > ops:: Index < ops:: Range < usize > > for Hmac < T > {
159
+ impl < T : Hash > ops:: Index < ops:: Range < usize > > for Hmac < T > {
162
160
type Output = [ u8 ] ;
163
161
fn index ( & self , index : ops:: Range < usize > ) -> & [ u8 ] {
164
162
& self . 0 [ index]
165
163
}
166
164
}
167
165
168
- impl < T : HashTrait > ops:: Index < ops:: RangeFrom < usize > > for Hmac < T > {
166
+ impl < T : Hash > ops:: Index < ops:: RangeFrom < usize > > for Hmac < T > {
169
167
type Output = [ u8 ] ;
170
168
fn index ( & self , index : ops:: RangeFrom < usize > ) -> & [ u8 ] {
171
169
& self . 0 [ index]
172
170
}
173
171
}
174
172
175
- impl < T : HashTrait > ops:: Index < ops:: RangeTo < usize > > for Hmac < T > {
173
+ impl < T : Hash > ops:: Index < ops:: RangeTo < usize > > for Hmac < T > {
176
174
type Output = [ u8 ] ;
177
175
fn index ( & self , index : ops:: RangeTo < usize > ) -> & [ u8 ] {
178
176
& self . 0 [ index]
179
177
}
180
178
}
181
179
182
- impl < T : HashTrait > ops:: Index < ops:: RangeFull > for Hmac < T > {
180
+ impl < T : Hash > ops:: Index < ops:: RangeFull > for Hmac < T > {
183
181
type Output = [ u8 ] ;
184
182
fn index ( & self , index : ops:: RangeFull ) -> & [ u8 ] {
185
183
& self . 0 [ index]
186
184
}
187
185
}
188
186
189
- impl < T : HashTrait > borrow:: Borrow < [ u8 ] > for Hmac < T > {
187
+ impl < T : Hash > borrow:: Borrow < [ u8 ] > for Hmac < T > {
190
188
fn borrow ( & self ) -> & [ u8 ] {
191
189
& self [ ..]
192
190
}
193
191
}
194
192
195
- impl < T : HashTrait > HashTrait for Hmac < T > {
193
+ impl < T : Hash > Hash for Hmac < T > {
196
194
type Engine = HmacEngine < T > ;
197
195
type Inner = T :: Inner ;
198
196
@@ -224,15 +222,15 @@ impl<T: HashTrait> HashTrait for Hmac<T> {
224
222
225
223
#[ cfg( feature = "serde" ) ]
226
224
#[ cfg_attr( docsrs, doc( cfg( feature = "serde" ) ) ) ]
227
- impl < T : HashTrait + Serialize > Serialize for Hmac < T > {
225
+ impl < T : Hash + Serialize > Serialize for Hmac < T > {
228
226
fn serialize < S : Serializer > ( & self , s : S ) -> Result < S :: Ok , S :: Error > {
229
227
Serialize :: serialize ( & self . 0 , s)
230
228
}
231
229
}
232
230
233
231
#[ cfg( feature = "serde" ) ]
234
232
#[ cfg_attr( docsrs, doc( cfg( feature = "serde" ) ) ) ]
235
- impl < ' de , T : HashTrait + Deserialize < ' de > > Deserialize < ' de > for Hmac < T > {
233
+ impl < ' de , T : Hash + Deserialize < ' de > > Deserialize < ' de > for Hmac < T > {
236
234
fn deserialize < D : Deserializer < ' de > > ( d : D ) -> Result < Hmac < T > , D :: Error > {
237
235
let inner = Deserialize :: deserialize ( d) ?;
238
236
Ok ( Hmac ( inner) )
@@ -244,7 +242,7 @@ mod tests {
244
242
#[ test]
245
243
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
246
244
fn test ( ) {
247
- use { sha256, HashEngine , HmacEngine , Hash , Hmac } ;
245
+ use crate :: { sha256, HashEngine , HmacEngine , Hash , Hmac } ;
248
246
249
247
#[ derive( Clone ) ]
250
248
struct Test {
@@ -370,7 +368,7 @@ mod tests {
370
368
#[ test]
371
369
fn hmac_sha512_serde ( ) {
372
370
use serde_test:: { Configure , Token , assert_tokens} ;
373
- use { sha512, Hash , Hmac } ;
371
+ use crate :: { sha512, Hash , Hmac } ;
374
372
375
373
static HASH_BYTES : [ u8 ; 64 ] = [
376
374
0x8b , 0x41 , 0xe1 , 0xb7 , 0x8a , 0xd1 , 0x15 , 0x21 ,
@@ -399,8 +397,7 @@ mod tests {
399
397
mod benches {
400
398
use test:: Bencher ;
401
399
402
- use sha256;
403
- use { Hmac , Hash , HashEngine } ;
400
+ use crate :: { Hmac , Hash , HashEngine , sha256} ;
404
401
405
402
#[ bench]
406
403
pub fn hmac_sha256_10 ( bh : & mut Bencher ) {
0 commit comments