General & Crypto
This commit is contained in:
parent
0bbcab5727
commit
02f94993c7
27 changed files with 1881 additions and 47 deletions
29
crypto/src/hash.rs
Normal file
29
crypto/src/hash.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use sha2::Digest;
|
||||
|
||||
pub fn sha256(data: &[u8]) -> [u8; 32] {
|
||||
let mut hasher = sha2::Sha256::new();
|
||||
hasher.update(data);
|
||||
let result = hasher.finalize();
|
||||
result.into()
|
||||
}
|
||||
|
||||
pub fn sha256_double(data: &[u8]) -> [u8; 32] {
|
||||
sha256(&sha256(data))
|
||||
}
|
||||
|
||||
pub struct Sha256Hasher(sha2::Sha256);
|
||||
|
||||
impl Sha256Hasher {
|
||||
pub fn new() -> Self {
|
||||
Self(sha2::Sha256::new())
|
||||
}
|
||||
|
||||
pub fn update(&mut self, data: &[u8]) {
|
||||
self.0.update(data);
|
||||
}
|
||||
|
||||
pub fn finalize(self) -> [u8; 32] {
|
||||
let result = self.0.finalize();
|
||||
result.into()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue