Skip to content

Commit

Permalink
feat: add support for blake2b256 and blake2s128 (multiformats#44)
Browse files Browse the repository at this point in the history
Adds support for blake2b256 and blake2s128.
  • Loading branch information
austinabell authored Feb 4, 2020
1 parent a8ab622 commit 9a945bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub enum Hash {
Keccak384,
/// Keccak-512 (64-byte hash size)
Keccak512,
/// Encoding unsupported
/// BLAKE2b-256 (32-byte hash size)
Blake2b256,
/// BLAKE2b-512 (64-byte hash size)
Blake2b512,
/// Encoding unsupported
/// BLAKE2s-128 (16-byte hash size)
Blake2s128,
/// BLAKE2s-256 (32-byte hash size)
Blake2s256,
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod hashes;

use std::convert::TryFrom;

use blake2b_simd::blake2b;
use blake2s_simd::blake2s;
use blake2b_simd::{blake2b, Params as Blake2bVariable};
use blake2s_simd::{blake2s, Params as Blake2sVariable};
use bytes::{BufMut, Bytes, BytesMut};
use sha2::Digest;
use tiny_keccak::Keccak;
Expand Down Expand Up @@ -42,6 +42,22 @@ macro_rules! encode {
let hash = $algorithm($input);
$output.copy_from_slice(hash.as_ref());
}};
(blake2_256, $constructor:ident, $input:expr, $output:expr) => {{
let hash = $constructor::new()
.hash_length(32)
.to_state()
.update($input)
.finalize();
$output.copy_from_slice(hash.as_ref());
}};
(blake2_128, $constructor:ident, $input:expr, $output:expr) => {{
let hash = $constructor::new()
.hash_length(16)
.to_state()
.update($input)
.finalize();
$output.copy_from_slice(hash.as_ref());
}};
}

// And another one to keep the matching DRY
Expand Down Expand Up @@ -113,7 +129,9 @@ pub fn encode(hash: Hash, input: &[u8]) -> Result<Multihash, EncodeError> {
Keccak384 => tiny::new_keccak384,
Keccak512 => tiny::new_keccak512,
Blake2b512 => blake2::blake2b,
Blake2b256 => blake2_256::Blake2bVariable,
Blake2s256 => blake2::blake2s,
Blake2s128 => blake2_128::Blake2sVariable,
});

Ok(Multihash {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fn multihash_encode() {
Keccak512, b"hello world", "1D403ee2b40047b8060f68c67242175660f4174d0af5c01d47168ec20ed619b0b7c42181f40aa1046f39e2ef9efc6910782a998e0013d172458957957fac9405b67d";
Blake2b512, b"hello world", "c0e40240021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0";
Blake2s256, b"hello world", "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b";
Blake2b256, b"hello world", "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610";
Blake2s128, b"hello world", "d0e4021037deae0226c30da2ab424a7b8ee14e83";
}
}

Expand Down Expand Up @@ -75,6 +77,8 @@ fn assert_decode() {
Keccak512, "1D403ee2b40047b8060f68c67242175660f4174d0af5c01d47168ec20ed619b0b7c42181f40aa1046f39e2ef9efc6910782a998e0013d172458957957fac9405b67d";
Blake2b512, "c0e40240021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbcc05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0";
Blake2s256, "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b";
Blake2b256, "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610";
Blake2s128, "d0e4021037deae0226c30da2ab424a7b8ee14e83";
}
}

Expand Down Expand Up @@ -209,6 +213,16 @@ fn multihash_methods() {
"e0e40220",
"9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b",
);
test_methods(
Hash::Blake2b256,
"a0e40220",
"256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610",
);
test_methods(
Hash::Blake2s128,
"d0e40210",
"37deae0226c30da2ab424a7b8ee14e83",
);
}

#[test]
Expand Down

0 comments on commit 9a945bb

Please sign in to comment.