Checksum Verifier — JavaScript source
Drag and drop a file to compute its MD5, SHA-1, SHA-256, and SHA-512 checksums. Paste an expected hash to verify integrity - detect tampered or corrupted downloads instantly. Runs entirely in your browser.
This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* checksum-verifier — MD5 / SHA-1 / SHA-256 / SHA-512 checksums + verification.
*
* Language: JavaScript (ES module; browsers, Node >= 16, Bun, Deno —
* crypto.subtle requires a secure context or localhost).
* CosmoDev polyglot showcase port of the `checksum-verifier` tool, ported from
* src/lib/checksum-verifier.ts.
*
* Same split as the TypeScript original: MD5 is rolled by hand (RFC 1321)
* because the Web Crypto API refuses to implement it, while SHA-1/256/512
* delegate to crypto.subtle.digest. The lib's incremental MD5 hasher is
* inlined into a one-shot digest here to stay compact; the verification
* helpers are ported as-is.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
// Per-round shift amounts (RFC 1321, section 3.4) and the sine table
// T[i] = floor(2^32 * |sin(i + 1)|) — IEEE-754 doubles make it deterministic.
const MD5_S = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
];
const MD5_K = new Int32Array(64).map((_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296));
/**
* One-shot MD5 of a byte array, as lowercase hex (RFC 1321).
* MD5 is only for integrity checks - not security.
*/
export function md5Hex(bytes) {
// Pad: the message, 0x80, zeros to 56 mod 64, then the 64-bit LE bit length.
const bitLenLo = (bytes.length * 8) >>> 0;
const bitLenHi = Math.floor(bytes.length / 536870912) >>> 0;
const total = (((bytes.length + 8) >> 6) + 1) << 6;
const block = new Uint8Array(total);
block.set(bytes);
block[bytes.length] = 0x80;
const view = new DataView(block.buffer);
view.setUint32(total - 8, bitLenLo, true);
view.setUint32(total - 4, bitLenHi, true);
let a0 = 0x67452301 | 0;
let b0 = 0xefcdab89 | 0;
let c0 = 0x98badcfe | 0;
let d0 = 0x10325476 | 0;
const m = new Int32Array(16);
for (let off = 0; off < total; off += 64) {
for (let i = 0; i < 16; i++) m[i] = view.getInt32(off + i * 4, true);
let a = a0;
let b = b0;
let c = c0;
let d = d0;
for (let i = 0; i < 64; i++) {
let f;
let g;
if (i < 16) {
f = (b & c) | (~b & d);
g = i;
} else if (i < 32) {
f = (d & b) | (~d & c);
g = (5 * i + 1) % 16;
} else if (i < 48) {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
} else {
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
const sum = (f + a + MD5_K[i] + m[g]) | 0;
const rot = MD5_S[i];
const tmp = d;
d = c;
c = b;
b = (b + ((sum << rot) | (sum >>> (32 - rot)))) | 0;
a = tmp;
}
a0 = (a0 + a) | 0;
b0 = (b0 + b) | 0;
c0 = (c0 + c) | 0;
d0 = (d0 + d) | 0;
}
let hex = '';
for (const word of [a0, b0, c0, d0])
for (let i = 0; i < 4; i++) hex += ((word >>> (8 * i)) & 0xff).toString(16).padStart(2, '0');
return hex;
}
function toHex(bytes) {
let s = '';
for (let i = 0; i < bytes.length; i++) s += bytes[i].toString(16).padStart(2, '0');
return s;
}
/** Digest a buffer with Web Crypto and return lowercase hex. */
export async function shaHex(algorithm, data) {
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
return toHex(new Uint8Array(await crypto.subtle.digest(algorithm, bytes)));
}
/** Compute all four checksums of a buffer (hand-rolled MD5 + 3 Web Crypto digests). */
export async function computeChecksums(data) {
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
const [sha1, sha256, sha512] = await Promise.all([
shaHex('SHA-1', bytes),
shaHex('SHA-256', bytes),
shaHex('SHA-512', bytes),
]);
return { md5: md5Hex(bytes), sha1, sha256, sha512 };
}
// --- Verification helpers ---------------------------------------------------
const LENGTH_TO_ALGO = { 32: 'md5', 40: 'sha1', 64: 'sha256', 128: 'sha512' };
/** Normalize a pasted hash: drop whitespace and `:` grouping, lowercase. */
function normalizeHash(hash) {
return hash.trim().replace(/[\s:]/g, '').toLowerCase();
}
/** Detect the algorithm from the hex length: 32=MD5, 40=SHA-1, 64=SHA-256, 128=SHA-512. */
export function detectHashAlgorithm(hash) {
const h = normalizeHash(hash);
if (!/^[0-9a-f]+$/.test(h)) return null;
return LENGTH_TO_ALGO[h.length] ?? null;
}
/**
* Compare an expected hash against a computed result. Returns null when the
* expected string is not a recognizable hex hash of a supported length.
*/
export function verifyChecksum(expected, result) {
const algorithm = detectHashAlgorithm(expected);
if (!algorithm) return null;
return { algorithm, match: normalizeHash(expected) === result[algorithm] };
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →