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.
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
Regex Generator
Generate regex patterns by describing what you need — 60+ curated patterns, visual composer, live tester, and pattern explanations
Git Command Builder
Build git commands visually with an interactive builder — branching, merging, rebasing, stashing, tags, and 80+ cheat sheet entries
CSP Header Builder
Build Content Security Policy headers visually with framework presets, security analysis, and multi-format output for Nginx, Apache, Vercel, Netlify
Code Screenshot Generator
Create beautiful code screenshots with 8 themes, syntax highlighting, customizable backgrounds, and window chrome — free Carbon/Ray.so alternative