From 38848362531a64ad98c46fed51758d993ddbef1c Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Tue, 15 Sep 2015 20:08:08 -0400 Subject: [PATCH 1/2] Derive some useful traits for HashTypes --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index fbbf650..81de3e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ use openssl::crypto::hash::{hash, Type}; /// List of types currently supported in Multihash. /// SHA3, Blake2b, and Blake2s are not yet supported in OpenSSL, so are not available in rust-multihash. +#[derive(PartialEq, Clone, Copy, Debug)] pub enum HashTypes { SHA1, SHA2256, From efa29001dfbbc5f06f3c8362274b4c142fd9579b Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Tue, 15 Sep 2015 20:09:09 -0400 Subject: [PATCH 2/2] Add a from_u8 utility function for HashTypes --- src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 81de3e5..0403cd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,19 @@ impl HashTypes { HashTypes::Blake2s => 0x41, } } + + /// Try to interpret a byte as a possible HashType + pub fn from_u8(b: u8) -> Option { + match b { + 0x11 => Some(HashTypes::SHA1), + 0x12 => Some(HashTypes::SHA2256), + 0x13 => Some(HashTypes::SHA2512), + 0x14 => Some(HashTypes::SHA3), + 0x40 => Some(HashTypes::Blake2b), + 0x41 => Some(HashTypes::Blake2s), + _ => None + } + } } /// Hashes the input using the given hash algorithm. Also adds the leading bytes for type of algo @@ -86,5 +99,8 @@ mod test { result.insert(1, length); assert_eq!(multihash(HashTypes::SHA2256, example.to_vec()).unwrap(), result); + + assert_eq!(HashTypes::from_u8(0x12), Some(HashTypes::SHA2256)); + assert_eq!(HashTypes::from_u8(0x01), None); } }