Created
May 2, 2025 15:01
-
-
Save colelawrence/1b08beb89096f2328042b358242522ca to your computer and use it in GitHub Desktop.
Hash string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* FNV-1a 32-bit hash – fast, non-cryptographic, browser-safe. | |
* Sufficient for generating repeatable fragment keys. | |
*/ | |
export const fnv1aHash32 = (str: string): string => { | |
let h = 0x811c9dc5; // FNV offset basis | |
for (let i = 0; i < str.length; i++) { | |
h ^= str.charCodeAt(i); | |
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24); | |
} | |
// Convert to unsigned 32-bit hex and pad to 8 chars | |
return (h >>> 0).toString(16).padStart(8, '0'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment