|
14 | 14 | // You should have received a copy of the GNU Affero General Public License |
15 | 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | 16 |
|
| 17 | +use blake2::digest::{Input, VariableOutput}; |
| 18 | +use blake2::VarBlake2b; |
17 | 19 | use primitives::{H128, H160, H256, H512}; |
18 | | -use rcrypto::blake2b::Blake2b; |
19 | | -use rcrypto::digest::Digest; |
20 | 20 |
|
21 | 21 | /// BLAKE128 |
22 | 22 | pub fn blake128<T: AsRef<[u8]>>(s: T) -> H128 { |
@@ -51,32 +51,27 @@ pub trait Blake { |
51 | 51 | } |
52 | 52 |
|
53 | 53 | macro_rules! implement_blake { |
54 | | - ($self:ident) => { |
| 54 | + ($self:ident, $size:expr) => { |
55 | 55 | impl Blake for $self { |
56 | 56 | fn blake<T: AsRef<[u8]>>(s: T) -> Self { |
57 | | - let input = s.as_ref(); |
58 | | - let mut result = Self::default(); |
59 | | - let mut hasher = Blake2b::new(result.len()); |
60 | | - hasher.input(input); |
61 | | - hasher.result(&mut *result); |
62 | | - result |
| 57 | + Self::blake_with_key(s, &[]) |
63 | 58 | } |
64 | 59 | fn blake_with_key<T: AsRef<[u8]>>(s: T, key: &[u8]) -> Self { |
65 | 60 | let input = s.as_ref(); |
66 | | - let mut result = Self::default(); |
67 | | - let mut hasher = Blake2b::new_keyed(result.len(), &key); |
| 61 | + let mut hasher = VarBlake2b::new_keyed(&key, $size); |
68 | 62 | hasher.input(input); |
69 | | - hasher.result(&mut *result); |
70 | | - result |
| 63 | + let mut result: [u8; $size] = [0; $size]; |
| 64 | + result.copy_from_slice(&hasher.vec_result()); |
| 65 | + Self(result) |
71 | 66 | } |
72 | 67 | } |
73 | 68 | }; |
74 | 69 | } |
75 | 70 |
|
76 | | -implement_blake!(H128); |
77 | | -implement_blake!(H160); |
78 | | -implement_blake!(H256); |
79 | | -implement_blake!(H512); |
| 71 | +implement_blake!(H128, 16); |
| 72 | +implement_blake!(H160, 20); |
| 73 | +implement_blake!(H256, 32); |
| 74 | +implement_blake!(H512, 64); |
80 | 75 |
|
81 | 76 | /// Get the 256-bits BLAKE2b hash of the empty bytes string. |
82 | 77 | pub const BLAKE_EMPTY: H256 = H256([ |
|
0 commit comments