implement better hashing

- use `blake2b_simd` crate and optimize
This commit is contained in:
Jörn-Michael Miehe 2025-06-24 19:07:22 +00:00
parent 6e553cc185
commit 173e9c38df
4 changed files with 46 additions and 60 deletions

View file

@ -1,2 +1,9 @@
[build]
target = "x86_64-unknown-linux-musl"
# rustflags = [
# # emit instructions tuned to the current CPU
# "-C", "target-cpu=native",
# # assume CPU features
# "-C", "target-feature=+avx2,+sse4.1,+ssse3,+aes",
# ]

76
Cargo.lock generated
View file

@ -67,6 +67,18 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "arrayref"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "base64"
version = "0.22.1"
@ -86,21 +98,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "blake2"
version = "0.10.6"
name = "blake2b_simd"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
checksum = "06e903a20b159e944f91ec8499fe1e55651480c541ea0a584f5d967c49ad9d99"
dependencies = [
"digest",
]
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
"arrayref",
"arrayvec",
"constant_time_eq",
]
[[package]]
@ -195,6 +200,12 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "constant_time_eq"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
[[package]]
name = "cookie"
version = "0.18.1"
@ -233,16 +244,6 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "ctrlc"
version = "3.4.7"
@ -273,17 +274,6 @@ dependencies = [
"thiserror 1.0.69",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@ -385,16 +375,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.16"
@ -902,7 +882,7 @@ name = "shrupl"
version = "0.1.0-alpha"
dependencies = [
"base64ct",
"blake2",
"blake2b_simd",
"clap",
"console",
"ctrlc",
@ -1045,12 +1025,6 @@ dependencies = [
"zerovec",
]
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.18"

View file

@ -6,7 +6,7 @@ description = "ShrUpl is a tool to upload files to a Sharry Instance through a p
[dependencies]
base64ct = { version = "1.8.0", default-features = false, features = ["alloc"] }
blake2 = { version = "0.10.6", default-features = false }
blake2b_simd = "1.0.3"
clap = { version = "4.5.38", features = ["derive"] }
console = { version = "0.15.11", default-features = false }
ctrlc = { version = "3.4.7", features = ["termination"] }
@ -22,5 +22,11 @@ thiserror = "2.0.12"
ureq = { version = "3.0.11", features = ["json"] }
[profile.release]
# Optimize for speed even more aggressively
opt-level = "z"
# better inlining
codegen-units = 1
# linkertime optimization
lto = true
debug = false
panic = "abort"

View file

@ -1,7 +1,7 @@
use std::{convert::Infallible, fmt, io, time::Duration};
use base64ct::{Base64UrlUnpadded, Encoding};
use blake2::{Blake2b, Digest, digest::consts::U16};
use blake2b_simd::Params as Blake2b;
use clap::{
Parser,
builder::{PossibleValuesParser, TypedValueParser},
@ -96,8 +96,6 @@ fn parse_sharry_file(data: &str) -> io::Result<Checked> {
Checked::new(data)
}
type Blake2b128 = Blake2b<U16>;
fn sorted<T>(values: &[T]) -> Vec<&T>
where
T: Ord,
@ -143,14 +141,15 @@ impl Cli {
}
pub fn get_hash(&self) -> String {
let mut hasher = Blake2b128::new();
hasher.update(self.get_uri());
hasher.update(&self.alias);
let mut hasher = Blake2b::new().hash_length(64).to_state();
hasher.update(self.get_uri().as_ref());
hasher.update(self.alias.as_bytes());
for chk in sorted(&self.files) {
hasher.update(chk);
hasher.update(chk.as_ref());
}
Base64UrlUnpadded::encode_string(&hasher.finalize())
Base64UrlUnpadded::encode_string(hasher.finalize().as_bytes())
}
}