mwhash
A fast, non-cryptographic u32 hash. no_std, zero dependencies, zero allocations.
Useful for: integrity checks on packets, checksums, fast buffer comparison, hash tables, embedded projects. Not suitable for cryptography.
Performance
Benchmark results (seed = 0x12345678) are shown below:
----------------------------------------------------------------------------------------------------
64 KB | size: 64.00 KB | iters: 20000 | total: 339.070ms | per call: 16.953µs | throughput: 3.87 GB/s
1 MB | size: 1024.00 KB | iters: 2000 | total: 549.020ms | per call: 274.509µs | throughput: 3.82 GB/s
1 GB | size: 1048576.00 KB | iters: 3 | total: 979.626ms | per call: 326.542ms | throughput: 3.29 GB/s
----------------------------------------------------------------------------------------------------
- 64 KB: L1/L2 cache.
- 1 MB: L3 cache.
- 1 GB: main memory.
Environment:* Tested on AMD64 architecture (Processor: [Intel Core Ultra 5 125h], RAM: [16 GB lpddr5x 6000MT/s]). Source code: examples/benchmark.rs
Installation
[]
= "0.1.0"
Examples
One-shot hashing
Use this when all your data is already in memory.
use mwhash;
let hash = mwhash;
println!;
assert_ne!;
Incremental hashing
Use this when data arrives in chunks — e.g. from a file or a network stream. No need to keep the whole buffer in memory.
use ;
let mut h = new;
h.update;
h.update;
let hash = h.finish;
println!;
assert_eq!;
Hashing with a numeric seed
The same input produces a different hash in a different "domain" — useful for salting data or separating hash spaces.
use ;
let mut h = with_seed;
h.update;
let h1 = h.finish;
println!;
assert_ne!;
Hashing with a string seed
More convenient than picking magic numbers by hand — the string itself is hashed into a numeric seed.
use ;
let mut h = with_string_seed;
h.update;
let hash = h.finish;
println!;
assert_ne!;
assert_ne!;
One-shot hashing with a seed
The fastest way to get a seeded hash when you don't need to process data in chunks.
use mwhash_seeded;
let data = b"test";
let seed = 0xDEAD_BEEF;
let hash = mwhash_seeded;
println!;
assert_ne!;
Concatenation without allocation
The order of updates doesn't matter — the result matches the hash of the combined data.
use ;
let part1 = b"foo";
let part2 = b"bar";
let combined_hash = mwhash_concat;
let full_hash = mwhash;
println!;
assert_eq!;
Reusing the Hasher
reset() returns the hasher to its initial state without new allocations — useful when processing many independent messages.
use ;
let custom_seed = 0x12345678;
let mut h = with_seed;
h.update;
let hash1 = h.finish;
println!;
h.reset;
h.update;
let hash2 = h.finish;
println!;
assert_eq!;
assert_ne!;
After reset(), a hasher with a custom seed behaves exactly as it did right after creation with that same seed:
use Hasher;
let custom_seed = 0x12345678;
let mut h = with_seed;
h.update;
println!;
h.reset;
assert_eq!;
h.update;
println!;
no_std
The library is fully no_std, with no alloc. It runs on any target, including bare metal.
License
Apache License 2.0. View License