start of architecture for different hash types
This commit is contained in:
56
src/hashes.rs
Normal file
56
src/hashes.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
mod null_hash;
|
||||
|
||||
use crate::Args;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
type BITS256 = [u8; 32];
|
||||
type BITS160 = [u8; 20];
|
||||
|
||||
type BITS32 = u32;
|
||||
|
||||
type ArcU8 = Arc<[u8]>;
|
||||
|
||||
macro_rules! arc_u8_sized {
|
||||
($size: literal) => {
|
||||
Arc::new([0u8, $size])
|
||||
};
|
||||
}
|
||||
|
||||
fn arc_u8_empty() -> ArcU8 {
|
||||
arc_u8_sized!(0)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum HashReturn {
|
||||
CRC32(BITS32),
|
||||
SHA256(BITS256),
|
||||
SHA3_256(BITS256),
|
||||
BLAKE256(BITS256),
|
||||
SHA1(BITS160),
|
||||
RAW(ArcU8),
|
||||
}
|
||||
|
||||
impl HashReturn {
|
||||
pub fn into_bytes(self) -> Arc<[u8]> {
|
||||
match self {
|
||||
HashReturn::CRC32(inner) => Arc::new(inner.to_le_bytes()),
|
||||
HashReturn::SHA256(inner) => Arc::new(inner),
|
||||
HashReturn::SHA3_256(inner) => Arc::new(inner),
|
||||
HashReturn::BLAKE256(inner) => Arc::new(inner),
|
||||
HashReturn::SHA1(inner) => Arc::new(inner),
|
||||
HashReturn::RAW(inner) => inner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum HashOp {
|
||||
Digest(ArcU8),
|
||||
Complete,
|
||||
}
|
||||
|
||||
struct HashKeeper {}
|
||||
|
||||
pub struct Hashes {
|
||||
hashes: Args,
|
||||
}
|
||||
@@ -37,8 +37,8 @@ impl NullHash {
|
||||
*loc = *byte;
|
||||
}
|
||||
}
|
||||
pub fn complete(self) -> BITS256 {
|
||||
self.hash
|
||||
pub fn complete(self) -> HashReturn {
|
||||
HashReturn::RAW(Arc::new(self.hash))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ mod test {
|
||||
hasher.digest([i; 1]);
|
||||
}
|
||||
|
||||
assert_eq!(*hasher.complete().first().unwrap(), 31);
|
||||
assert_eq!(*hasher.complete().into_bytes().first().unwrap(), 31);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -68,7 +68,7 @@ mod test {
|
||||
|
||||
hasher.digest(source_bytes);
|
||||
|
||||
let result = hasher.complete();
|
||||
let result = hasher.complete().into_bytes();
|
||||
|
||||
let start = 255 - 32u8;
|
||||
for (byte, i) in result.iter().zip(start..=255) {
|
||||
|
||||
Reference in New Issue
Block a user