Skip to content

Commit 33fa64a

Browse files
SeungMin Leesgkim126
authored andcommitted
Replace the blake crate to other crate
1 parent 86f19ed commit 33fa64a

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sha2 = "0.8.1"
1414
sha3 = "0.8.2"
1515
ripemd160 = "0.8.0"
1616
digest = "0.8"
17+
blake2 = "0.8.1"
1718

1819
[dev-dependencies]
1920
rand = "0.6.1"

src/blake.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
use blake2::digest::{Input, VariableOutput};
18+
use blake2::VarBlake2b;
1719
use primitives::{H128, H160, H256, H512};
18-
use rcrypto::blake2b::Blake2b;
19-
use rcrypto::digest::Digest;
2020

2121
/// BLAKE128
2222
pub fn blake128<T: AsRef<[u8]>>(s: T) -> H128 {
@@ -51,32 +51,27 @@ pub trait Blake {
5151
}
5252

5353
macro_rules! implement_blake {
54-
($self:ident) => {
54+
($self:ident, $size:expr) => {
5555
impl Blake for $self {
5656
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, &[])
6358
}
6459
fn blake_with_key<T: AsRef<[u8]>>(s: T, key: &[u8]) -> Self {
6560
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);
6862
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)
7166
}
7267
}
7368
};
7469
}
7570

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);
8075

8176
/// Get the 256-bits BLAKE2b hash of the empty bytes string.
8277
pub const BLAKE_EMPTY: H256 = H256([

0 commit comments

Comments
 (0)