Skip to content

Instantly share code, notes, and snippets.

@thomastheyoung
Created March 27, 2025 21:46
Show Gist options
  • Save thomastheyoung/0ad2fe5df9db616a6efaaf866dd854fb to your computer and use it in GitHub Desktop.
Save thomastheyoung/0ad2fe5df9db616a6efaaf866dd854fb to your computer and use it in GitHub Desktop.
/**
* 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