Skip to main content

varar_core/
hash.rs

1//! FNV-1a (32-bit) change-detector over UTF-16 code units — port of `hash.ts` /
2//! `Hash.java`. Byte-identical across every port so `varar.lock.json` fingerprints
3//! match. The `fnv1a:` prefix namespaces the algorithm.
4
5const FNV_OFFSET: u32 = 0x811c_9dc5;
6const FNV_PRIME: u32 = 0x0100_0193;
7
8/// Hashes `source` to `fnv1a:<8 hex>` (FNV-1a over UTF-16 code units, wrapping).
9pub fn hash_source(source: &str) -> String {
10    let mut h: u32 = FNV_OFFSET;
11    for unit in source.encode_utf16() {
12        h = (h ^ u32::from(unit)).wrapping_mul(FNV_PRIME);
13    }
14    // `{:08x}` formats the 32-bit pattern as unsigned lowercase hex.
15    format!("fnv1a:{h:08x}")
16}