From 7365ce9bb5509b244997c33d3c00538c0b4ccf05 Mon Sep 17 00:00:00 2001 From: Cadyn Date: Sun, 7 Feb 2021 22:30:23 -0800 Subject: [PATCH] Add XXHash64 to hashing functions --- Cargo.lock | 33 +++++++++++++++++++++++++++------ Cargo.toml | 4 +++- dmsrc/hash.dm | 1 + src/hash.rs | 11 +++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f4922b44..8895a377 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -199,9 +199,9 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] name = "const-random" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02dc82c12dc2ee6e1ded861cf7d582b46f66f796d1b6c93fa28b911ead95da02" +checksum = "f590d95d011aa80b063ffe3253422ed5aa462af4e9867d43ce8337562bac77c4" dependencies = [ "const-random-macro", "proc-macro-hack", @@ -209,12 +209,14 @@ dependencies = [ [[package]] name = "const-random-macro" -version = "0.1.11" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc757bbb9544aa296c2ae00c679e81f886b37e28e59097defe0cf524306f6685" +checksum = "615f6e27d000a2bffbc7f2f6a8669179378fa27ee4d0a509e985dfc0a7defb40" dependencies = [ "getrandom 0.2.0", + "lazy_static", "proc-macro-hack", + "tiny-keccak", ] [[package]] @@ -301,6 +303,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "dashmap" version = "3.11.10" @@ -1585,6 +1593,7 @@ name = "rust-g" version = "0.4.7" dependencies = [ "chrono", + "const-random", "dashmap", "flume", "git2", @@ -1604,6 +1613,7 @@ dependencies = [ "sha-1", "sha2 0.9.1", "thiserror", + "twox-hash", "url", "zip", ] @@ -2014,6 +2024,15 @@ dependencies = [ "syn", ] +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tinyvec" version = "0.3.4" @@ -2099,11 +2118,13 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "twox-hash" -version = "1.5.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" +checksum = "04f8ab788026715fa63b31960869617cba39117e520eb415b0139543e325ab59" dependencies = [ + "cfg-if", "rand 0.7.3", + "static_assertions", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index f78da0e3..07ecbc7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,8 @@ thiserror = "1.0" flume = { version = "0.9", optional = true } chrono = { version = "0.4", optional = true } md-5 = { version = "0.9", optional = true } +twox-hash = { version = "1.6.0", optional = true } +const-random = { version = "0.1.13", optional = true } sha-1 = { version = "0.9", optional = true } sha2 = { version = "0.9", optional = true } hex = { version = "0.4", optional = true } @@ -54,7 +56,7 @@ log = ["chrono"] sql = ["mysql", "serde", "serde_json", "once_cell", "dashmap", "jobs"] # non-default features -hash = ["md-5", "sha-1", "sha2", "hex"] +hash = ["md-5", "sha-1", "sha2", "hex", "twox-hash", "const-random"] url = ["url-dep", "percent-encoding"] unzip = ["zip", "jobs"] diff --git a/dmsrc/hash.dm b/dmsrc/hash.dm index 878ab395..3ecedc6f 100644 --- a/dmsrc/hash.dm +++ b/dmsrc/hash.dm @@ -5,6 +5,7 @@ #define RUSTG_HASH_SHA1 "sha1" #define RUSTG_HASH_SHA256 "sha256" #define RUSTG_HASH_SHA512 "sha512" +#define RUSTG_HASH_XXH64 "xxh64" #ifdef RUSTG_OVERRIDE_BUILTINS #define md5(thing) (isfile(thing) ? rustg_hash_file(RUSTG_HASH_MD5, "[thing]") : rustg_hash_string(RUSTG_HASH_MD5, thing)) diff --git a/src/hash.rs b/src/hash.rs index 05e53098..15914912 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -1,12 +1,18 @@ use crate::error::{Error, Result}; +use const_random::const_random ; +const XXHASH_SEED: u64 = const_random!(u64); +use twox_hash::XxHash64; use md5::Md5; use sha1::Sha1; use sha2::{Digest, Sha256, Sha512}; use std::{ fs::File, io::{BufReader, Read}, + hash::Hasher, }; + + byond_fn! { hash_string(algorithm, string) { string_hash(algorithm, string).ok() } } @@ -37,6 +43,11 @@ fn hash_algorithm>(name: &str, bytes: B) -> Result { hasher.update(bytes.as_ref()); Ok(hex::encode(hasher.finalize())) } + "xxh64" => { + let mut hasher = XxHash64::with_seed(XXHASH_SEED); + hasher.write(bytes.as_ref()); + Ok(format!("{:x}",hasher.finish())) + } _ => Err(Error::InvalidAlgorithm), } }