converted to trait
This commit is contained in:
@@ -22,7 +22,7 @@ fn arc_u8_empty() -> ArcU8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum HashReturn {
|
pub enum HashReturn {
|
||||||
CRC32(BITS32),
|
CRC32(BITS32),
|
||||||
SHA256(BITS256),
|
SHA256(BITS256),
|
||||||
SHA3_256(BITS256),
|
SHA3_256(BITS256),
|
||||||
@@ -44,9 +44,10 @@ impl HashReturn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum HashOp {
|
pub trait Hasher {
|
||||||
Digest(ArcU8),
|
fn new() -> Self;
|
||||||
Complete,
|
fn digest(&mut self, bytes: ArcU8);
|
||||||
|
fn complete(self) -> HashReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HashKeeper {}
|
struct HashKeeper {}
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ pub struct NullHash {
|
|||||||
hash: BITS256,
|
hash: BITS256,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NullHash {
|
impl Hasher for NullHash {
|
||||||
pub fn new() -> Self {
|
fn new() -> Self {
|
||||||
NullHash { hash: [0; 32] }
|
NullHash { hash: [0; 32] }
|
||||||
}
|
}
|
||||||
pub fn digest(&mut self, bytes: impl AsRef<[u8]>) {
|
fn digest(&mut self, bytes: ArcU8) {
|
||||||
let bytes = bytes.as_ref();
|
let bytes = bytes.as_ref();
|
||||||
|
|
||||||
let start_point = if bytes.len() < 32 {
|
let start_point = if bytes.len() < 32 {
|
||||||
@@ -37,7 +37,7 @@ impl NullHash {
|
|||||||
*loc = *byte;
|
*loc = *byte;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn complete(self) -> HashReturn {
|
fn complete(self) -> HashReturn {
|
||||||
HashReturn::RAW(Arc::new(self.hash))
|
HashReturn::RAW(Arc::new(self.hash))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,7 @@ mod test {
|
|||||||
let mut hasher = NullHash::new();
|
let mut hasher = NullHash::new();
|
||||||
|
|
||||||
for i in 0..32u8 {
|
for i in 0..32u8 {
|
||||||
hasher.digest([i; 1]);
|
hasher.digest(Arc::new([i; 1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(*hasher.complete().into_bytes().first().unwrap(), 31);
|
assert_eq!(*hasher.complete().into_bytes().first().unwrap(), 31);
|
||||||
@@ -66,7 +66,7 @@ mod test {
|
|||||||
*byte = i;
|
*byte = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
hasher.digest(source_bytes);
|
hasher.digest(Arc::new(source_bytes));
|
||||||
|
|
||||||
let result = hasher.complete().into_bytes();
|
let result = hasher.complete().into_bytes();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user