JavaScript SHA-256 Hash Generator
Generate SHA-256 hashes and test them against your JavaScript output. Paste text to hash it instantly, then use the code examples for browser (Web Crypto API) and Node.js (crypto module) implementations. All hashing is client-side.
Hash Generator
Generate SHA-1, SHA-256, SHA-384, and SHA-512 hashes using the Web Crypto API.
How to generate SHA-256 hashes in JavaScript
Browser (Web Crypto API): async function sha256(message) { const encoder = new TextEncoder(); const data = encoder.encode(message); const hash = await crypto.subtle.digest('SHA-256', data); return Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, '0')).join(''); }. Node.js: const crypto = require('crypto'); const hash = crypto.createHash('sha256').update('Hello World').digest('hex'). Note: Web Crypto API is async (returns a Promise); Node.js crypto is synchronous by default.
// JavaScript — generate SHA-256 hash
// Browser (Web Crypto API)
async function sha256(text) {
const data = new TextEncoder().encode(text);
const hash = await crypto.subtle.digest("SHA-256", data);
return Array.from(new Uint8Array(hash))
.map(b => b.toString(16).padStart(2, "0")).join("");
}
const hash = await sha256("Hello, World!");
console.log(hash);
// "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f"
// Node.js
import { createHash } from "crypto";
const hash = createHash("sha256").update("Hello, World!").digest("hex");SHA-256 hashing patterns in web development
Common use cases: Subresource Integrity (SRI) for script tags — generate a SHA-256 hash of your CDN scripts and add integrity='sha256-...' to prevent tampering. Content hashing for cache busting — hash file contents to generate unique URLs. Webhook verification — HMAC-SHA256 to validate incoming webhooks from Stripe, GitHub, or Slack. In Node.js: const hmac = crypto.createHmac('sha256', secret).update(body).digest('hex'). For client-side, Web Crypto API also supports HMAC: crypto.subtle.sign('HMAC', key, data).
SHA-256 performance in JavaScript
Web Crypto API is significantly faster than pure JavaScript implementations because it uses native C/C++ code under the hood. For Node.js, the built-in crypto module is also native. Avoid pure JS libraries like js-sha256 unless you need compatibility with very old browsers. In Web Workers, crypto.subtle is available for off-main-thread hashing. For hashing large files: read in chunks with FileReader or ReadableStream, and use crypto.subtle.digest() on the complete ArrayBuffer, or use Node.js createHash() with stream.pipe().
Frequently Asked Questions
How do I generate a SHA-256 hash in the browser?
Use the Web Crypto API: const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)). Convert the ArrayBuffer result to hex: Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, '0')).join('').
Can I use SHA-256 synchronously in Node.js?
Yes. Node.js crypto is synchronous: require('crypto').createHash('sha256').update(data).digest('hex'). For async streaming of large files, use createHash() with readable.pipe().
How do I verify a webhook signature with SHA-256 in JavaScript?
Node.js: const expected = crypto.createHmac('sha256', secret).update(requestBody).digest('hex'); if (crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(signature))) { /* valid */ }. Use timingSafeEqual() to prevent timing attacks.
Related Generate Tools
HTML Table Generator
Build HTML tables visually with an interactive editor — add rows, columns, header rows, captions, and styling. Export as plain HTML, inline CSS, or Tailwind classes
CSS Clip-path Generator
Create CSS clip-path shapes visually — circle, ellipse, inset, or polygon with draggable points. 13 shape presets, interactive preview, and production-ready CSS output
CSS Filter Generator
Build CSS filter effects visually — blur, brightness, contrast, grayscale, hue-rotate, invert, opacity, saturate, sepia, and drop-shadow with 12 presets and live preview
UUID Generator
Generate random UUID v4 identifiers in bulk