@alwatr/hash-string
Version:
A simple utility to generate a hash string.
8 lines (7 loc) • 2.22 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../src/main.ts"],
"sourcesContent": ["/**\n * Simple hash string for fast hashing (like md5).\n * This function is not very secure and should not be used for security purposes.\n * But it cannot be reversed easily and brute force can take up to years for fast computers.\n *\n * @param str - The string or number to hash\n * @param prefix - A prefix to add to the beginning of the hash result\n * @param repeat - Number of times to repeat the hashing process for increased complexity (default: 3)\n * @returns A hashed string with the specified prefix\n */\nexport function nanoHash(str: string | number, prefix: string, repeat = 1): string {\n if (repeat < 1) {\n throw new Error('The repeat parameter must be greater than or equal to 1');\n }\n\n let hash1 = 0xdeadbeef;\n let hash2 = 0x41c6ce57;\n\n if (typeof str === 'number') {\n str = str.toString();\n }\n\n const len = str.length;\n for (let i = 0; i < len; i++) {\n const char = str.charCodeAt(i);\n hash1 = Math.imul(hash1 ^ char, 2654435761);\n hash2 = Math.imul(hash2 ^ char, 1597334677);\n }\n\n hash1 = Math.imul(hash1 ^ (hash1 >>> 16), 2246822507) ^ Math.imul(hash2 ^ (hash2 >>> 13), 3266489909);\n hash2 = Math.imul(hash2 ^ (hash2 >>> 16), 2246822507) ^ Math.imul(hash1 ^ (hash1 >>> 13), 3266489909);\n\n const result = prefix + (hash1 >>> 0).toString(36) + (hash2 >>> 0).toString(36);\n if (repeat === 1) {\n return result;\n }\n else {\n return nanoHash(result, prefix, repeat - 1);\n }\n}\n"],
"mappings": ";;qqBAAA,6GAUO,SAAS,SAAS,IAAsB,OAAgB,OAAS,EAAW,CACjF,GAAI,OAAS,EAAG,CACd,MAAM,IAAI,MAAM,yDAAyD,CAC3E,CAEA,IAAI,MAAQ,WACZ,IAAI,MAAQ,WAEZ,GAAI,OAAO,MAAQ,SAAU,CAC3B,IAAM,IAAI,SAAS,CACrB,CAEA,MAAM,IAAM,IAAI,OAChB,QAAS,EAAI,EAAG,EAAI,IAAK,IAAK,CAC5B,MAAM,KAAO,IAAI,WAAW,CAAC,EAC7B,MAAQ,KAAK,KAAK,MAAQ,KAAM,UAAU,EAC1C,MAAQ,KAAK,KAAK,MAAQ,KAAM,UAAU,CAC5C,CAEA,MAAQ,KAAK,KAAK,MAAS,QAAU,GAAK,UAAU,EAAI,KAAK,KAAK,MAAS,QAAU,GAAK,UAAU,EACpG,MAAQ,KAAK,KAAK,MAAS,QAAU,GAAK,UAAU,EAAI,KAAK,KAAK,MAAS,QAAU,GAAK,UAAU,EAEpG,MAAM,OAAS,QAAU,QAAU,GAAG,SAAS,EAAE,GAAK,QAAU,GAAG,SAAS,EAAE,EAC9E,GAAI,SAAW,EAAG,CAChB,OAAO,MACT,KACK,CACH,OAAO,SAAS,OAAQ,OAAQ,OAAS,CAAC,CAC5C,CACF",
"names": []
}