Use unsigned-varints, add BLAKE2 support in multihash (#525)

* Add support for unsigned varints

* Depend on unsigned-varint 0.2 without default features

* Change hash code type from u8 to u64

* Fix hash codes and enum variants for BLAKE2 to fit the standard (see #524)

* Run cargo fmt on crate

* Expand hash_types test to include all variants

* Add support for BLAKE2b-512 and BLAKE2s-256

* Depend on blake2 crate 0.7 with no default features

* Update encode! macro for support for blake2 crate

* Update all tests to include BLAKE2b-512 and BLAKE2s-256

* Reduce hash code size from u64 to u16

* Fix typo in doc comment

* Bump tiny-keccak to version 1.4

* Remove unnecessary default-features = false in Cargo.toml
This commit is contained in:
Eyal Kalderon
2018-10-01 15:58:11 +08:00
committed by Toralf Wittner
parent 4fa680e282
commit ad0807b3f3
5 changed files with 95 additions and 57 deletions

View File

@ -1,4 +1,3 @@
/// List of types currently supported in the multihash spec.
///
/// Not all hash types are supported by this library.
@ -26,15 +25,19 @@ pub enum Hash {
Keccak384,
/// Keccak-512 (64-byte hash size)
Keccak512,
/// BLAKE2b-512 (64-byte hash size)
Blake2b512,
/// Encoding unsupported
Blake2b,
Blake2b256,
/// BLAKE2s-256 (32-byte hash size)
Blake2s256,
/// Encoding unsupported
Blake2s,
Blake2s128,
}
impl Hash {
/// Get the corresponding hash code.
pub fn code(&self) -> u8 {
pub fn code(&self) -> u16 {
match *self {
Hash::SHA1 => 0x11,
Hash::SHA2256 => 0x12,
@ -47,8 +50,10 @@ impl Hash {
Hash::Keccak256 => 0x1B,
Hash::Keccak384 => 0x1C,
Hash::Keccak512 => 0x1D,
Hash::Blake2b => 0x40,
Hash::Blake2s => 0x41,
Hash::Blake2b512 => 0xB240,
Hash::Blake2b256 => 0xB220,
Hash::Blake2s256 => 0xB260,
Hash::Blake2s128 => 0xB250,
}
}
@ -66,13 +71,15 @@ impl Hash {
Hash::Keccak256 => 32,
Hash::Keccak384 => 48,
Hash::Keccak512 => 64,
Hash::Blake2b => 64,
Hash::Blake2s => 32,
Hash::Blake2b512 => 64,
Hash::Blake2b256 => 32,
Hash::Blake2s256 => 32,
Hash::Blake2s128 => 16,
}
}
/// Returns the algorithm corresponding to a code, or `None` if no algorith is matching.
pub fn from_code(code: u8) -> Option<Hash> {
/// Returns the algorithm corresponding to a code, or `None` if no algorithm is matching.
pub fn from_code(code: u16) -> Option<Hash> {
Some(match code {
0x11 => Hash::SHA1,
0x12 => Hash::SHA2256,
@ -85,8 +92,10 @@ impl Hash {
0x1B => Hash::Keccak256,
0x1C => Hash::Keccak384,
0x1D => Hash::Keccak512,
0x40 => Hash::Blake2b,
0x41 => Hash::Blake2s,
0xB240 => Hash::Blake2b512,
0xB220 => Hash::Blake2b256,
0xB260 => Hash::Blake2s256,
0xB250 => Hash::Blake2s128,
_ => return None,
})
}