Add blake2 and xxhash hash functions

This commit is contained in:
Alex 2021-02-23 17:52:28 +01:00
parent 1abbca37c4
commit e8e4418ca7
3 changed files with 156 additions and 17 deletions

147
Cargo.lock generated
View file

@ -71,6 +71,17 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blake2"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a5720225ef5daecf08657f23791354e1685a8c91a4c60c7f3d3b2892f978f4"
dependencies = [
"crypto-mac 0.8.0",
"digest 0.9.0",
"opaque-debug 0.3.0",
]
[[package]]
name = "block-buffer"
version = "0.7.3"
@ -234,7 +245,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5"
dependencies = [
"generic-array 0.12.3",
"subtle",
"subtle 1.0.0",
]
[[package]]
name = "crypto-mac"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab"
dependencies = [
"generic-array 0.14.4",
"subtle 2.4.0",
]
[[package]]
@ -288,6 +309,28 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]]
name = "fasthash"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "032213946b4eaae09117ec63f020322b78ca7a31d8aa2cf64df3032e1579690f"
dependencies = [
"cfg-if 0.1.10",
"fasthash-sys",
"num-traits",
"seahash",
"xoroshiro128",
]
[[package]]
name = "fasthash-sys"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6de941abfe2e715cdd34009d90546f850597eb69ca628ddfbf616e53dda28f8"
dependencies = [
"gcc",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -314,6 +357,12 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
@ -450,7 +499,7 @@ dependencies = [
"hex",
"log",
"pretty_env_logger",
"rand",
"rand 0.7.3",
"rmp-serde",
"serde",
"sha2",
@ -467,7 +516,7 @@ dependencies = [
"base64 0.13.0",
"bytes 0.4.12",
"chrono",
"crypto-mac",
"crypto-mac 0.7.0",
"err-derive",
"futures",
"futures-util",
@ -483,7 +532,7 @@ dependencies = [
"log",
"md-5",
"percent-encoding",
"rand",
"rand 0.7.3",
"roxmltree",
"sha2",
"tokio",
@ -505,7 +554,7 @@ dependencies = [
"garage_util 0.1.1",
"hex",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"serde",
"serde_bytes",
@ -530,7 +579,7 @@ dependencies = [
"garage_util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"hex",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"serde",
"serde_bytes",
@ -554,7 +603,7 @@ dependencies = [
"hyper",
"hyper-rustls",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"rustls",
"serde",
@ -582,7 +631,7 @@ dependencies = [
"hyper",
"hyper-rustls",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"rustls",
"serde",
@ -607,7 +656,7 @@ dependencies = [
"hex",
"hexdump",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"serde",
"serde_bytes",
@ -631,7 +680,7 @@ dependencies = [
"hex",
"hexdump",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"serde",
"serde_bytes",
@ -643,14 +692,16 @@ dependencies = [
name = "garage_util"
version = "0.1.1"
dependencies = [
"blake2",
"err-derive",
"fasthash",
"futures",
"futures-util",
"hex",
"http",
"hyper",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"roxmltree",
"rustls",
@ -676,7 +727,7 @@ dependencies = [
"http",
"hyper",
"log",
"rand",
"rand 0.7.3",
"rmp-serde",
"roxmltree",
"rustls",
@ -707,6 +758,12 @@ dependencies = [
"roxmltree",
]
[[package]]
name = "gcc"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
[[package]]
name = "generic-array"
version = "0.12.3"
@ -813,7 +870,7 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695"
dependencies = [
"crypto-mac",
"crypto-mac 0.7.0",
"digest 0.8.1",
]
@ -1310,6 +1367,19 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi 0.3.9",
]
[[package]]
name = "rand"
version = "0.7.3"
@ -1319,7 +1389,7 @@ dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_core 0.5.1",
"rand_hc",
]
@ -1330,9 +1400,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.5.1"
@ -1348,7 +1433,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
@ -1461,6 +1555,12 @@ dependencies = [
"untrusted",
]
[[package]]
name = "seahash"
version = "3.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f57ca1d128a43733fd71d583e837b1f22239a37ebea09cde11d8d9a9080f47"
[[package]]
name = "serde"
version = "1.0.119"
@ -1597,6 +1697,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee"
[[package]]
name = "subtle"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2"
[[package]]
name = "syn"
version = "1.0.58"
@ -2005,3 +2111,12 @@ name = "xmlparser"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "114ba2b24d2167ef6d67d7d04c8cc86522b87f490025f39f0303b7db5bf5e3d8"
[[package]]
name = "xoroshiro128"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0eeda34baec49c4f1eb2c04d59b761582fd6330010f9330ca696ca1a355dfcd"
dependencies = [
"rand 0.4.6",
]

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"

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()
}