Skip to main content

Crate mwhash

Crate mwhash 

Source
Expand description

src/lib.rs mwhash - a simple, fast, non-cryptographic hash function for Rust

§Usage Examples

§1. One-shot hashing

Use this when you have all your data ready and just need a quick hash.

use mwhash::mwhash;
 
let hash = mwhash(b"hello mwhash");
println!("Result: {:08x}", hash);
 
assert_ne!(hash, 0); 

§2. Incremental hashing

Use this when your data arrives in chunks (like reading from a file or network). It saves memory because you don’t need to keep the whole input in RAM.

use mwhash::{Hasher, mwhash};

let mut h = Hasher::new();
h.update(b"hello");
h.update(b" rust"); // println!("hash: {:08x}", h.finish());
 
// Must call .finish() to finalize the calculation
let hash = h.finish();
println!("Incremental hash: {:08x}", hash);
 
assert_eq!(hash, mwhash(b"hello rust"));

§3. Hashing with a custom seed

Use this to create unique hashes for different contexts or to “salt” your data so identical inputs produce different outputs.

use mwhash::{mwhash, Hasher};

let mut h = Hasher::with_seed(0x12345678);
h.update(b"codeberg better than github");
let h1 = h.finish();
 
println!("Seeded hash: {:08x}", h1);
// The hash with a seed will be different from the default one
assert_ne!(h1, mwhash(b"codeberg better than github"));

§4. Hashing with a custom string seed

Use this for a more human-friendly approach. The string is automatically hashed to become the numeric seed.

use mwhash::{mwhash, Hasher};

let mut h = Hasher::with_string_seed("My-SID-is-definitely-unique");
h.update(b"100% essential data");
let hash = h.finish();
println!("String-seeded hash: {:08x}", hash);
 
assert_ne!(hash, 0);
assert_ne!(hash, mwhash(b"100% essential data"));

§5. Your SID is definitely better than 0x9E3779B9?

Because typing 0x9E3779B9 is boring, and “My-SID-is-definitely-unique” is a statement of intent!

§6. Concatenation helper

If you are processing data in parts, you can easily combine them. This shows that the order of updates doesn’t change the final result.

use mwhash::{mwhash, mwhash_concat};

let part1 = b"foo";
let part2 = b"bar";
 
// Both approaches yield the exact same hash
let combined_hash = mwhash_concat(part1, part2);
let full_hash = mwhash(b"foobar");

println!("Hash: {:08x}", combined_hash);
assert_eq!(combined_hash, full_hash);

§7. One-shot hashing with a custom seed

Use this when you have data and a seed, but don’t need to process data in multiple chunks. It’s the fastest way to get a seeded hash.

use mwhash::mwhash_seeded;

let data = b"test";
let seed = 0xDEAD_BEEF;
 
let hash = mwhash_seeded(data, seed);
println!("Hash: {:08x}", hash);
 
// Verify that it's not the default hash
assert_ne!(hash, mwhash::mwhash(data));

§8. Reusing the Hasher

You can reuse the same Hasher instance by calling .reset(). This is more efficient than creating a new instance for every hash operation because it reuses existing memory.

use mwhash::{mwhash_seeded, Hasher};

let custom_seed = 0x12345678;
let mut h = Hasher::with_seed(custom_seed);

// First usage
h.update(b"data1");
let hash1 = h.finish();
println!("Hash 1: {:08x}", hash1);

// Reset and reuse
h.reset();
h.update(b"data2");
let hash2 = h.finish();
println!("Hash 2: {:08x}", hash2);

assert_eq!(hash2, mwhash_seeded(b"data2", custom_seed));
assert_ne!(hash1, hash2);

§9. Resetting the Hasher

You can reset a Hasher to reuse it without reallocating memory. After a reset, the Hasher behaves exactly as it did when it was first created with the same seed.

use mwhash::Hasher;

let custom_seed = 0x12345678;
let mut h = Hasher::with_seed(custom_seed);

// 1. Use the hasher
h.update(b"some data");
println!("Hash: {:08x}", h.finish());
 
// 2. Reset the hasher
h.reset();
 
// 3. It's now empty again, behaving like a fresh instance
// An empty hasher with a seed should equal mwhash_seeded(b"", seed)
assert_eq!(h.finish(), mwhash::mwhash_seeded(b"", custom_seed));
 
// 4. Reuse it for new data
h.update(b"new data");
println!("New hash after reset: {:08x}", h.finish());

Structs§

Hasher

Functions§

mwhash
mwhash_concat
mwhash_seeded
mwhash_u32
mwhash_u64