add blake2 256/512 impls
This commit is contained in:
@@ -25,6 +25,9 @@ pub struct Args {
|
|||||||
/// Blake2b256
|
/// Blake2b256
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub blake256: bool,
|
pub blake256: bool,
|
||||||
|
/// Blake2b512
|
||||||
|
#[arg(long)]
|
||||||
|
pub blake512: bool,
|
||||||
/// Blake3
|
/// Blake3
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub blake3: bool,
|
pub blake3: bool,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
mod blake256;
|
mod blake256;
|
||||||
|
|
||||||
mod blake3;
|
mod blake3;
|
||||||
|
mod blake512;
|
||||||
mod crc32;
|
mod crc32;
|
||||||
mod md5;
|
mod md5;
|
||||||
mod null_hash;
|
mod null_hash;
|
||||||
@@ -11,6 +13,8 @@ use crate::Args;
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
type BITS512 = [u8; 64];
|
||||||
|
|
||||||
type BITS256 = [u8; 32];
|
type BITS256 = [u8; 32];
|
||||||
type BITS160 = [u8; 20];
|
type BITS160 = [u8; 20];
|
||||||
|
|
||||||
@@ -30,12 +34,17 @@ fn arc_u8_empty() -> ArcU8 {
|
|||||||
arc_u8_sized!(0)
|
arc_u8_sized!(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bits512_default() -> BITS512 {
|
||||||
|
[0; 64]
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum HashReturn {
|
pub enum HashReturn {
|
||||||
CRC32(BITS32),
|
CRC32(BITS32),
|
||||||
SHA256(BITS256),
|
SHA256(BITS256),
|
||||||
SHA3_256(BITS256),
|
SHA3_256(BITS256),
|
||||||
BLAKE256(BITS256),
|
BLAKE256(BITS256),
|
||||||
|
BLAKE512(BITS512),
|
||||||
BLAKE3(BITS256),
|
BLAKE3(BITS256),
|
||||||
SHA1(BITS160),
|
SHA1(BITS160),
|
||||||
MD5(BITS128),
|
MD5(BITS128),
|
||||||
@@ -49,6 +58,7 @@ impl HashReturn {
|
|||||||
HashReturn::SHA256(inner) => Arc::new(inner),
|
HashReturn::SHA256(inner) => Arc::new(inner),
|
||||||
HashReturn::SHA3_256(inner) => Arc::new(inner),
|
HashReturn::SHA3_256(inner) => Arc::new(inner),
|
||||||
HashReturn::BLAKE256(inner) => Arc::new(inner),
|
HashReturn::BLAKE256(inner) => Arc::new(inner),
|
||||||
|
HashReturn::BLAKE512(inner) => Arc::new(inner),
|
||||||
HashReturn::BLAKE3(inner) => Arc::new(inner),
|
HashReturn::BLAKE3(inner) => Arc::new(inner),
|
||||||
HashReturn::SHA1(inner) => Arc::new(inner),
|
HashReturn::SHA1(inner) => Arc::new(inner),
|
||||||
HashReturn::MD5(inner) => Arc::new(inner),
|
HashReturn::MD5(inner) => Arc::new(inner),
|
||||||
|
|||||||
@@ -1,20 +1,30 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use ::blake2::Blake2bVarCore;
|
||||||
|
use ::blake2::digest::{Update, VariableOutput};
|
||||||
|
use blake2::digest::core_api::RtVariableCoreWrapper;
|
||||||
|
|
||||||
pub struct BLAKE256 {
|
pub struct BLAKE256 {
|
||||||
hash: BITS256,
|
hash: BITS256,
|
||||||
|
hasher: RtVariableCoreWrapper<Blake2bVarCore>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hasher for BLAKE256 {
|
impl Hasher for BLAKE256 {
|
||||||
fn new() -> Self {
|
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]>) {
|
fn digest(&mut self, bytes: impl AsRef<[u8]>) {
|
||||||
todo!()
|
self.hasher.update(bytes.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete(self) -> HashReturn {
|
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]
|
#[test]
|
||||||
fn test_blake256_hash() {
|
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