The string to hash
A hexadecimal string representing the hash
const hash = generateSimpleHash("test string");
console.log(hash); // "a5d7d2a9"
export function generateSimpleHash(str: string): string {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash; // Convert to 32bit integer
}
return Math.abs(hash).toString(16);
}
Generates a simple hash from a string using the djb2 algorithm. This is a non-cryptographic hash function suitable for request identification.