How to Compute SHA-256 & SHA-512 File Checksums 100% in the Browser with Web Crypto API (Zero Uploads)
Whenever you download an OS ISO, release binary, firmware package, or client-supplied archive, verifying its SHA-256 checksum is the fastest way to confirm the file has not been corrupted or tampered with. Yet developer
Whenever you download an OS ISO, release binary, firmware package, or client-supplied archive, verifying its SHA-256 checksum is the fastest way to confirm the file has not been corrupted or tampered with.
Yet developers still do two unnecessary things:
- Upload sensitive internal files to random "online hash checker" websites (sending the file over the network!).
- Pull heavy npm packages like
crypto-jsinto their frontend bundle just to hash aFileorBlob.
Every modern browser ships with the Web Crypto API (window.crypto.subtle) - native, C++/Rust hardware-accelerated hashing with zero dependencies and zero server uploads.
Here is the clean TypeScript implementation we use in production, plus three subtle gotchas that catch frontend engineers off guard.
Why Native crypto.subtle.digest() Beats JS Crypto Libraries
Before installing an npm package for hashing, look at the trade-offs:
| Approach | Bundle Cost | Speed on 50 MB File | Hardware SHA-NI Acceleration |
|---|---|---|---|
crypto-js (pure JS) |
~80-150 KB | Slow (blocks main thread) | No |
| WASM hasher | ~30-90 KB | Fast | Limited |
Native crypto.subtle.digest() |
0 KB | ~40-90 ms (async off-thread) | Yes (native browser crypto) |
Because crypto.subtle.digest() runs asynchronously in the browser's native crypto layer, hashing a typical release ZIP or installer finishes in milliseconds without locking up React state updates.
Step 1: Hash Any Local File in 20 Lines of TypeScript
Here is a drop-in helper supporting SHA-256, SHA-512, SHA-384, and legacy SHA-1:
export type WebCryptoAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
export async function hashFileClientSide(
file: File,
algorithm: WebCryptoAlgorithm = "SHA-256"
): Promise<string> {
if (!window.isSecureContext || !window.crypto?.subtle) {
throw new Error(
"Web Crypto API requires HTTPS or localhost (secure context)."
);
}
const buffer = await file.arrayBuffer();
const digest = await window.crypto.subtle.digest(algorithm, buffer);
// Convert ArrayBuffer to lowercase hexadecimal string
const bytes = new Uint8Array(digest);
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
}
Notice padStart(2, "0")!
A surprisingly common bug in custom hex converters is forgetting .padStart(2, "0"). Any byte below 0x10 (like 0x0a) turns into "a" instead of "0a", producing a broken 61-character SHA-256 string that never matches sha256sum!
Step 2: Computing All Hashes in Parallel (Zero Extra File Reads)
If your UI displays SHA-1, SHA-256, and SHA-512 side by side, read the file into memory once, then run all digests concurrently with Promise.all:
export interface FileHashBundle {
sha1: string;
sha256: string;
sha512: string;
sizeBytes: number;
elapsedMs: number;
}
function bufferToHex(buf: ArrayBuffer): string {
return Array.from(new Uint8Array(buf), (b) =>
b.toString(16).padStart(2, "0")
).join("");
}
export async function computeAllFileHashes(
file: File
): Promise<FileHashBundle> {
const start = performance.now();
const buffer = await file.arrayBuffer();
const [sha1Buf, sha256Buf, sha512Buf] = await Promise.all([
crypto.subtle.digest("SHA-1", buffer),
crypto.subtle.digest("SHA-256", buffer),
crypto.subtle.digest("SHA-512", buffer),
]);
return {
sha1: bufferToHex(sha1Buf),
sha256: bufferToHex(sha256Buf),
sha512: bufferToHex(sha512Buf),
sizeBytes: file.size,
elapsedMs: Math.round(performance.now() - start),
};
}
Step 3: Safe Checksum Comparison (Normalization + Constant-Time Check)
Users frequently paste checksums copied from GitHub release pages or terminal outputs that include uppercase hex, leading/trailing spaces, or filenames (e3b0c442... release.zip).
Normalize the expected hash first, then compare without leaking timing differences:
export function normalizeHexHash(input: string): string {
// Take first token in case user pasted full `sha256sum` output ("<hash> filename.zip")
const firstToken = input.trim().split(/\s+/)[0] || "";
return firstToken.toLowerCase().replace(/^0x/, "");
}
export function timingSafeEqualHex(a: string, b: string): boolean {
const normA = normalizeHexHash(a);
const normB = normalizeHexHash(b);
if (!normA || normA.length !== normB.length) return false;
let diff = 0;
for (let i = 0; i < normA.length; i++) {
diff |= normA.charCodeAt(i) ^ normB.charCodeAt(i);
}
return diff === 0;
}
3 Production Gotchas to Watch Out For
-
crypto.subtleisundefinedon plainhttp://LAN IPs: It works onhttps://andhttp://localhost, but if you test from your phone overhttp://192.168.1.x:3000,window.crypto.subtlewill beundefined. Always guard withwindow.isSecureContextand show a clear message. -
MD5 is intentionally omitted from Web Crypto API: Because MD5 is cryptographically broken for collision resistance, browsers refuse to expose it in
crypto.subtle. Only fall back to a small pure-TS MD5 routine if users explicitly need legacy checksum verification. -
Very large files (1 GB+ ISOs):
file.arrayBuffer()loads the entire file into RAM at once. For multi-gigabyte ISOs, process chunks viafile.stream().getReader()with a streaming hasher inside a Web Worker so mobile browsers never hit OOM limits.
Try It Live (Zero Uploads)
We run this exact browser-native verification pipeline in production so files never leave your machine:
- OmniTools File Hash & Checksum Verifier - Compute MD5, SHA-1, SHA-256, and SHA-512 hashes and verify against expected checksums 100% locally.
-
OmniTools UUID v4 Generator - Uses native
crypto.randomUUID()/crypto.getRandomValues()for cryptographically secure IDs in bulk.
Key Takeaways
- Stop shipping 100 KB crypto libraries just to compute SHA-256 or SHA-512 in the browser.
- Read
file.arrayBuffer()once and hash multiple algorithms concurrently viaPromise.all. - Always check
window.isSecureContext, pad hex bytes with.padStart(2, "0"), and handle pastedsha256sumlines cleanly.
Do you verify checksums via terminal (sha256sum / CertUtil) or browser tools in your workflow? Let me know in the comments!
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.