Syntax
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));Examples
Computing a SHA-256 Digest: working example
Run this standalone example in a modern browser console unless another environment is specified. Expected result: On HTTPS or localhost, the console prints ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad.
(async () => {
const input = new TextEncoder().encode("abc");
const digest = await crypto.subtle.digest("SHA-256", input);
const hex = Array.from(new Uint8Array(digest), b => b.toString(16).padStart(2, "0")).join("");
console.log(hex);
})();- Expected result: On HTTPS or localhost, the console prints ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad.
- Text is explicitly encoded as UTF-8 before hashing.
- The 32-byte digest is rendered as 64 hexadecimal characters.
Best practices
- Do not use a fast general-purpose digest alone for password storage.
- Use authenticated mechanisms when adversaries could replace both content and its expected digest.
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification