21 lines
569 B
TypeScript
21 lines
569 B
TypeScript
export const hash = async (text: string) => {
|
|
const msgBuffer = new TextEncoder().encode(text);
|
|
|
|
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
|
|
|
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
|
|
return hashHex;
|
|
}
|
|
|
|
export const hashSync = (text: string) => {
|
|
let hash = 5381;
|
|
let i = 0;
|
|
for (const char of text) {
|
|
hash = ((hash << 5) - hash) + char.charCodeAt(0);
|
|
hash |= 0;
|
|
}
|
|
return hash.toString(16);
|
|
}
|