Skip to content

Instantly share code, notes, and snippets.

@erkobridee
Created May 7, 2026 16:45
Show Gist options
  • Select an option

  • Save erkobridee/91e5d912d1a861686e8c01bf549f85eb to your computer and use it in GitHub Desktop.

Select an option

Save erkobridee/91e5d912d1a861686e8c01bf549f85eb to your computer and use it in GitHub Desktop.
/*
based on:
https://sqlpey.com/javascript/top-5-methods-to-generate-a-hash-from-a-string-in-javascript/#generating-a-hash-method-3-using-hexadecimal-output
*/
export const cyrb53Hex = (str: string, seed = 0) => {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i)
h1 = Math.imul(h1 ^ ch, 2654435761)
h2 = Math.imul(h2 ^ ch, 1597334677)
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
return ((h2 >>> 0).toString(16).padStart(8, '0') + (h1 >>> 0).toString(16).padStart(8, '0'))
}
export const simpleStringHash = (str: string) => cyrb53Hex(str)
export const simpleObjectHash = (value: unknown) => cyrb53Hex(JSON.stringify(value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment