Add blake2 and xxhash hash functions

This commit is contained in:
Alex Auvolat
2021-02-23 17:52:28 +01:00
parent 1abbca37c4
commit e8e4418ca7
3 changed files with 156 additions and 17 deletions
+2
View File
@@ -16,8 +16,10 @@ path = "lib.rs"
rand = "0.7"
hex = "0.3"
sha2 = "0.8"
blake2 = "0.9"
err-derive = "0.2.3"
log = "0.4"
fasthash = "0.4"
sled = "0.31"
+23 -1
View File
@@ -1,7 +1,6 @@
use rand::Rng;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};
@@ -78,6 +77,8 @@ pub type UUID = FixedBytes32;
pub type Hash = FixedBytes32;
pub fn sha256sum(data: &[u8]) -> Hash {
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.input(data);
let mut hash = [0u8; 32];
@@ -85,6 +86,27 @@ pub fn sha256sum(data: &[u8]) -> Hash {
hash.into()
}
pub fn blake2sum(data: &[u8]) -> Hash {
use blake2::{Blake2b, Digest};
let mut hasher = Blake2b::new();
hasher.update(data);
let mut hash = [0u8; 32];
hash.copy_from_slice(&hasher.finalize()[..32]);
hash.into()
}
pub type FastHash = u64;
pub fn fasthash(data: &[u8]) -> FastHash {
use std::hash::Hasher;
use fasthash::{xx::Hasher64, FastHasher};
let mut h = Hasher64::new();
h.write(data);
h.finish()
}
pub fn gen_uuid() -> UUID {
rand::thread_rng().gen::<[u8; 32]>().into()
}