@@ -27,11 +27,12 @@ pub(crate) mod backend;
2727pub mod many;
2828pub ( crate ) mod state;
2929
30+ mod hash;
3031mod params;
3132#[ cfg( test) ]
3233mod test;
3334
34- pub use self :: { params:: Params , state:: State } ;
35+ pub use self :: { hash :: Hash , params:: Params , state:: State } ;
3536
3637use core:: { fmt, mem:: size_of} ;
3738
@@ -96,76 +97,6 @@ pub fn blake2b(input: &[u8]) -> Hash {
9697 Params :: new ( ) . hash ( input)
9798}
9899
99- type HexString = arrayvec:: ArrayString < [ u8 ; 2 * OUTBYTES ] > ;
100-
101- /// A finalized BLAKE2 hash, with constant-time equality.
102- #[ derive( Clone , Copy ) ]
103- pub struct Hash {
104- pub ( crate ) bytes : [ u8 ; OUTBYTES ] ,
105- pub ( crate ) len : u8 ,
106- }
107-
108- impl Hash {
109- /// Convert the hash to a byte slice. Note that if you're using BLAKE2 as a MAC, you need
110- /// constant time equality, which `&[u8]` doesn't provide.
111- pub fn as_bytes ( & self ) -> & [ u8 ] {
112- & self . bytes [ ..self . len as usize ]
113- }
114-
115- /// Convert the hash to a byte array. Note that if you're using BLAKE2 as a
116- /// MAC, you need constant time equality, which arrays don't provide. This
117- /// panics in debug mode if the length of the hash isn't `OUTBYTES`.
118- #[ inline]
119- pub fn as_array ( & self ) -> & [ u8 ; OUTBYTES ] {
120- debug_assert_eq ! ( self . len as usize , OUTBYTES ) ;
121- & self . bytes
122- }
123-
124- /// Convert the hash to a lowercase hexadecimal
125- /// [`ArrayString`](https://docs.rs/arrayvec/0.4/arrayvec/struct.ArrayString.html).
126- pub fn to_hex ( self ) -> HexString {
127- bytes_to_hex ( self . as_bytes ( ) )
128- }
129- }
130-
131- fn bytes_to_hex ( bytes : & [ u8 ] ) -> HexString {
132- let mut s = arrayvec:: ArrayString :: new ( ) ;
133- let table = b"0123456789abcdef" ;
134- for & b in bytes {
135- s. push ( table[ ( b >> 4 ) as usize ] as char ) ;
136- s. push ( table[ ( b & 0xf ) as usize ] as char ) ;
137- }
138- s
139- }
140-
141- /// This implementation is constant time, if the two hashes are the same length.
142- impl PartialEq for Hash {
143- fn eq ( & self , other : & Hash ) -> bool {
144- constant_time_eq:: constant_time_eq ( self . as_bytes ( ) , other. as_bytes ( ) )
145- }
146- }
147-
148- /// This implementation is constant time, if the slice is the same length as the hash.
149- impl PartialEq < [ u8 ] > for Hash {
150- fn eq ( & self , other : & [ u8 ] ) -> bool {
151- constant_time_eq:: constant_time_eq ( self . as_bytes ( ) , other)
152- }
153- }
154-
155- impl Eq for Hash { }
156-
157- impl AsRef < [ u8 ] > for Hash {
158- fn as_ref ( & self ) -> & [ u8 ] {
159- self . as_bytes ( )
160- }
161- }
162-
163- impl fmt:: Debug for Hash {
164- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
165- write ! ( f, "Hash(0x{})" , self . to_hex( ) )
166- }
167- }
168-
169100// Paint a byte pattern that won't repeat, so that we don't accidentally miss
170101// buffer offset bugs. This is the same as what Bao uses in its tests.
171102#[ cfg( test) ]
0 commit comments