From 6dc885cfe66e99628d31fd4998ee5453dacd433b Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 14 Apr 2024 10:42:45 -0400 Subject: [PATCH] No thread locals for aneri-crypto --- crates/crypto/src/lib.rs | 79 ++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/crates/crypto/src/lib.rs b/crates/crypto/src/lib.rs index 2d0844b..112d7bf 100644 --- a/crates/crypto/src/lib.rs +++ b/crates/crypto/src/lib.rs @@ -12,12 +12,44 @@ extern crate meowtonin; use ::digest::{FixedOutputReset, Update}; use blake3::Hasher as Blake3; use md5::{Digest, Md5}; +use once_cell::sync::Lazy; +use parking_lot::Mutex; use sha1::Sha1; use sha2::{Sha224, Sha256, Sha384, Sha512, Sha512_224, Sha512_256}; use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; -use std::{cell::RefCell, path::PathBuf, thread::LocalKey}; +use std::path::PathBuf; use xxhash_rust::{xxh3::Xxh3, xxh32::Xxh32, xxh64::Xxh64}; +macro_rules! impl_hasher { + ($(($name:ident, $hasher:ty, $initializer:expr)),+) => { + $( + impl_hasher!($name, $hasher, $initializer); + )+ + }; + ($name:ident, $hasher:ty, $initializer:expr) => { + static $name: Lazy> = Lazy::new(|| Mutex::new($initializer)); + }; +} + +impl_hasher!( + (BLAKE3, Blake3, Blake3::new()), + (MD5, Md5, Md5::new()), + (SHA1, Sha1, Sha1::new()), + (SHA224, Sha224, Sha224::new()), + (SHA256, Sha256, Sha256::new()), + (SHA384, Sha384, Sha384::new()), + (SHA512, Sha512, Sha512::new()), + (SHA512_224, Sha512_224, Sha512_224::new()), + (SHA512_256, Sha512_256, Sha512_256::new()), + (SHA3_224, Sha3_224, Sha3_224::new()), + (SHA3_256, Sha3_256, Sha3_256::new()), + (SHA3_384, Sha3_384, Sha3_384::new()), + (SHA3_512, Sha3_512, Sha3_512::new()), + (XXH32, Xxh32, Xxh32::new(0)), + (XXH64, Xxh64, Xxh64::new(0)), + (XXH3, Xxh3, Xxh3::new()) +); + #[byond_fn] pub fn hash(algorithm: String, data: String) -> Option { hash_data(algorithm, data) @@ -29,25 +61,6 @@ pub fn hash_file(algorithm: String, file: PathBuf) -> Option { hash_data(algorithm, data) } -thread_local! { - static BLAKE3: RefCell = RefCell::new(Blake3::new()); - static MD5: RefCell = RefCell::new(Md5::new()); - static SHA1: RefCell = RefCell::new(Sha1::new()); - static SHA224: RefCell = RefCell::new(Sha224::new()); - static SHA256: RefCell = RefCell::new(Sha256::new()); - static SHA384: RefCell = RefCell::new(Sha384::new()); - static SHA512: RefCell = RefCell::new(Sha512::new()); - static SHA512_224: RefCell = RefCell::new(Sha512_224::new()); - static SHA512_256: RefCell = RefCell::new(Sha512_256::new()); - static SHA3_224: RefCell = RefCell::new(Sha3_224::new()); - static SHA3_256: RefCell = RefCell::new(Sha3_256::new()); - static SHA3_384: RefCell = RefCell::new(Sha3_384::new()); - static SHA3_512: RefCell = RefCell::new(Sha3_512::new()); - static XXH32: RefCell = RefCell::new(Xxh32::new(0)); - static XXH64: RefCell = RefCell::new(Xxh64::new(0)); - static XXH3: RefCell = RefCell::new(Xxh3::new()); -} - fn hash_data(algorithm: impl AsRef, data: impl AsRef<[u8]>) -> Option { let algorithm = algorithm.as_ref(); let data = data.as_ref(); @@ -65,24 +78,27 @@ fn hash_data(algorithm: impl AsRef, data: impl AsRef<[u8]>) -> Option Some(digest_hash(&SHA3_256, data)), "sha3_384" => Some(digest_hash(&SHA3_384, data)), "sha3_512" => Some(digest_hash(&SHA3_512, data)), - "xxh32" => XXH32.with_borrow_mut(|xxh| { + "xxh32" => { + let mut xxh = XXH32.lock(); xxh.reset(0); xxh.update(data); let hash = xxh.digest(); Some(faster_hex::hex_string(&hash.to_le_bytes())) - }), - "xxh64" => XXH64.with_borrow_mut(|xxh| { + } + "xxh64" => { + let mut xxh = XXH64.lock(); xxh.reset(0); xxh.update(data); let hash = xxh.digest(); Some(faster_hex::hex_string(&hash.to_le_bytes())) - }), - "xxh3" => XXH3.with_borrow_mut(|xxh| { + } + "xxh3" => { + let mut xxh = XXH3.lock(); xxh.reset(); xxh.update(data); let hash = xxh.digest(); Some(faster_hex::hex_string(&hash.to_le_bytes())) - }), + } "crc32" => Some(faster_hex::hex_string(&crc32fast::hash(data).to_le_bytes())), "crc32c" => Some(faster_hex::hex_string(&crc32c::crc32c(data).to_le_bytes())), _ => None, @@ -90,16 +106,15 @@ fn hash_data(algorithm: impl AsRef, data: impl AsRef<[u8]>) -> Option( - local_hasher: &'static LocalKey>, + lazy_hasher: &'static Lazy>, input: Bytes, ) -> String where Hasher: Update + FixedOutputReset, Bytes: AsRef<[u8]>, { - local_hasher.with_borrow_mut(|hasher| { - hasher.update(input.as_ref()); - let hash = hasher.finalize_fixed_reset(); - faster_hex::hex_string(&hash) - }) + let mut hasher = lazy_hasher.lock(); + hasher.update(input.as_ref()); + let hash = hasher.finalize_fixed_reset(); + faster_hex::hex_string(&hash) }