Take in any javascript object and return the sha-256 representation of that object as a hex-encoded string.

Readme
1
2
3
4
5
6
7
export const hash = async (o: unknown): Promise<string> => {
const stringInput = typeof o === "string" ? o : JSON.stringify(o);
const arrayInput = new Uint8Array(new TextEncoder().encode(stringInput));
const hashBuffer = await crypto.subtle.digest("sha-256", arrayInput);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.