diff options
author | Alex Auvolat <alex@adnab.me> | 2021-02-23 17:52:28 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-02-23 17:52:28 +0100 |
commit | e8e4418ca7c0b1156d5bc103b164aa72ecb748b9 (patch) | |
tree | e370f48960256b10b50cfbcbae81b27cb88dd6ab /src/util/data.rs | |
parent | 1abbca37c4c320bafbbaad8cd4d4155372526a86 (diff) | |
download | garage-e8e4418ca7c0b1156d5bc103b164aa72ecb748b9.tar.gz garage-e8e4418ca7c0b1156d5bc103b164aa72ecb748b9.zip |
Add blake2 and xxhash hash functions
Diffstat (limited to 'src/util/data.rs')
-rw-r--r-- | src/util/data.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/util/data.rs b/src/util/data.rs index 657467ba..a1c292e7 100644 --- a/src/util/data.rs +++ b/src/util/data.rs @@ -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() } |