FileDummy Logo
FileDummy
File Security & Integrity

How to Hash a File in JavaScript: SHA-256, MD5, and SHA-1

Calculate file checksums in JavaScript using the Web Crypto API (SHA-256) and crypto-js (MD5, SHA-1). Browser and Node.js examples with sample test files.

September 14, 20269 min read1,420 views
javascriptcryptographysha256checksumweb-crypto

Calculating cryptographic hashes of files is fundamental for data deduplication, tamper verification, and integrity checks. Modern browsers provide the high-performance Web Crypto API for SHA-256 and SHA-512, while Node.js offers the native crypto module with streaming support.

Web Crypto API (Browser SHA-256)

The Web Crypto API runs inside the browser engine using hardware-accelerated cryptographic primitives:

TypeScript
export async function calculateFileSha256(file: File): Promise<string> {
  const arrayBuffer = await file.arrayBuffer();
  const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);

  // Convert ArrayBuffer to Hex string
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  const hexHash = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');

  return hexHash;
}
Verified Test Asset.txt

Download our 1MB text file with deterministic content to test your SHA-256 hash algorithm.

Download 1MB TXT Sample →

Streaming Hashes in Node.js (Large Files)

When hashing multi-gigabyte files in Node.js, loading the entire file into memory with fs.readFileSync causes out-of-memory crashes. Use streaming transforms instead:

TypeScript
import fs from 'fs';
import crypto from 'crypto';

export function getFileChecksum(filePath: string, algorithm = 'sha256'): Promise<string> {
  return new Promise((resolve, reject) => {
    const hash = crypto.createHash(algorithm);
    const stream = fs.createReadStream(filePath);

    stream.on('data', (chunk) => hash.update(chunk));
    stream.on('end', () => resolve(hash.digest('hex')));
    stream.on('error', (err) => reject(err));
  });
}

When to Use MD5 vs SHA-256

  • MD5: Cryptographically broken. Do not use for password hashing, digital signatures, or security checks. Still widely used for non-security checksums, such as AWS S3 ETag verification and local file caching keys.
  • SHA-256: Strong cryptographic collision resistance. The industry benchmark for software releases, package integrity, and blockchain transactions.
NDL

Nguyen Dai Long

Author

Backend Lead • Distributed Systems & Cloud Edge Architecture Specialist

4+ years designing high-throughput file ingestion pipelines, database architectures, and distributed edge storage on Cloudflare R2 & AWS S3. Founder of FileDummy and the NDL Ecosystem.

Was this article helpful?

Click Like to support the author and help other developers discover this guide.

Engineering Discussion & Feedback (0)

Share benchmark results, report edge cases, or ask technical questions.

0/3000

No comments yet. Be the first developer to start the discussion!

Frequently Asked Questions

Should I use MD5 or SHA-256 for file integrity checks?â–¾

For security-critical applications, always use SHA-256 or SHA-512. MD5 is cryptographically broken and should only be used for non-security checksums like deduplication or cache keys.

How do I hash a large file without loading it into memory?â–¾

In Node.js use crypto.createHash('sha256') as a Transform stream with pipe(). In the browser, use the Web Crypto API with SubtleCrypto.digest() processing chunks via ReadableStream.

Can I compute a file hash entirely in the browser?â–¾

Yes. The Web Crypto API (crypto.subtle.digest) is available in all modern browsers. Read the file using FileReader as an ArrayBuffer, then pass it to subtle.digest('SHA-256'). MD5 requires a third-party library since it is not in the Web Crypto API.

Related Articles