Created
March 27, 2025 21:46
-
-
Save thomastheyoung/0ad2fe5df9db616a6efaaf866dd854fb to your computer and use it in GitHub Desktop.
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
/** | |
* Generates a non-cryptographic hash from a string using the FNV-1a algorithm. | |
* Returns the hash as a hexadecimal string. | |
* Note: This is for non-cryptographic purposes only. | |
*/ | |
export function FNVHash(input: string): string { | |
let hash = 2166136261; // FNV offset basis | |
for (let i = 0; i < input.length; i++) { | |
hash ^= input.charCodeAt(i); | |
// Math.imul performs 32-bit multiplication which is critical for FNV-1a | |
hash = Math.imul(hash, 16777619); | |
} | |
// Convert to an unsigned 32-bit integer and then to hexadecimal | |
return (hash >>> 0).toString(16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment