add blake2 256/512 impls
This commit is contained in:
@@ -1,20 +1,30 @@
|
||||
use super::*;
|
||||
use ::blake2::Blake2bVarCore;
|
||||
use ::blake2::digest::{Update, VariableOutput};
|
||||
use blake2::digest::core_api::RtVariableCoreWrapper;
|
||||
|
||||
pub struct BLAKE256 {
|
||||
hash: BITS256,
|
||||
hasher: RtVariableCoreWrapper<Blake2bVarCore>,
|
||||
}
|
||||
|
||||
impl Hasher for BLAKE256 {
|
||||
fn new() -> Self {
|
||||
BLAKE256 { hash: [0; 32] }
|
||||
BLAKE256 {
|
||||
hash: [0; 32],
|
||||
hasher: ::blake2::Blake2bVar::new(32).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn digest(&mut self, bytes: impl AsRef<[u8]>) {
|
||||
todo!()
|
||||
self.hasher.update(bytes.as_ref())
|
||||
}
|
||||
|
||||
fn complete(self) -> HashReturn {
|
||||
todo!()
|
||||
let res = self.hasher.finalize_boxed();
|
||||
let mut ret = BITS256::default();
|
||||
ret.copy_from_slice(&res);
|
||||
HashReturn::BLAKE256(ret)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +34,17 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_blake256_hash() {
|
||||
todo!();
|
||||
let input = "HelloWorld";
|
||||
let expected_output = "27159ce7d992c98fb04d5e9a59e43e75f77882b676fc6b2ccb8e952c2373da3e";
|
||||
|
||||
let mut blake2 = BLAKE256::new();
|
||||
|
||||
blake2.digest(input);
|
||||
|
||||
let result = blake2.complete().into_bytes();
|
||||
|
||||
let hash_out = crate::hex_table::u8_array_to_lower_hex_string(&result).unwrap();
|
||||
|
||||
assert_eq!(hash_out, expected_output);
|
||||
}
|
||||
}
|
||||
|
||||
52
src/hashes/blake512.rs
Normal file
52
src/hashes/blake512.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use super::*;
|
||||
use ::blake2::Blake2bVarCore;
|
||||
use ::blake2::digest::{Update, VariableOutput};
|
||||
use blake2::digest::core_api::RtVariableCoreWrapper;
|
||||
|
||||
pub struct BLAKE512 {
|
||||
hash: BITS512,
|
||||
hasher: RtVariableCoreWrapper<Blake2bVarCore>,
|
||||
}
|
||||
|
||||
impl Hasher for BLAKE512 {
|
||||
fn new() -> Self {
|
||||
BLAKE512 {
|
||||
hash: [0; 64],
|
||||
// Variable size was done instead of using Blake2b512 type alias as the concrete type
|
||||
// that would need to be defined in the struct BLAKE512 is silly
|
||||
hasher: ::blake2::Blake2bVar::new(64).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn digest(&mut self, bytes: impl AsRef<[u8]>) {
|
||||
self.hasher.update(bytes.as_ref())
|
||||
}
|
||||
|
||||
fn complete(self) -> HashReturn {
|
||||
let res = self.hasher.finalize_boxed();
|
||||
let mut ret = bits512_default();
|
||||
ret.copy_from_slice(&res);
|
||||
HashReturn::BLAKE512(ret)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_blake512_hash() {
|
||||
let input = "HelloWorld";
|
||||
let expected_output = "8dc77b2e140c3601a9fdd146684dea960124c514b999314be65fafe189cecee9bb1395cc80826aa1b8464de775678d13bfd332c51aafd026b9b5a67e606430f3";
|
||||
|
||||
let mut blake2 = BLAKE512::new();
|
||||
|
||||
blake2.digest(input);
|
||||
|
||||
let result = blake2.complete().into_bytes();
|
||||
|
||||
let hash_out = crate::hex_table::u8_array_to_lower_hex_string(&result).unwrap();
|
||||
|
||||
assert_eq!(hash_out, expected_output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user