Hashing Files with SHA-256

Hash files with SHA-256 directly in your browser using the Web Crypto API. Learn how file hashing works, stream processing for large files, and practical applications.

SHA-256

Detailed Explanation

Hashing a file with SHA-256 means computing a unique 256-bit fingerprint of the file's entire contents. This process reads every byte of the file and produces a fixed-length digest that changes completely if even a single byte is modified. Modern browsers can perform this operation entirely client-side using the Web Crypto API.

Browser-based file hashing:

The Web Crypto API provides crypto.subtle.digest('SHA-256', buffer) which accepts an ArrayBuffer and returns the hash. For files, you read them using the FileReader API or the newer File API with file.arrayBuffer(). The hash computation runs natively in the browser's crypto implementation, which is typically as fast as command-line tools since it uses the same underlying C/C++ libraries.

Handling large files:

For small files (under 100MB), reading the entire file into memory and hashing at once is fine. For large files, streaming is necessary to avoid memory exhaustion. While the Web Crypto API's digest() method does not directly support streaming, you can use incremental hashing libraries or the experimental ReadableStream approach. Some implementations read the file in chunks (e.g., 1MB at a time) and use a WebAssembly SHA-256 implementation that supports incremental updates.

Use cases for file hashing:

Developers hash files to detect changes in build systems and caches (a changed hash triggers a rebuild). Cloud storage services hash files to detect duplicates (content-addressable storage). Forensic analysts hash evidence files to prove chain of custody (the hash proves the file was not altered). Backup systems use file hashes to perform incremental backups efficiently. Blockchain systems hash files to create tamper-proof records.

Comparing hashes across platforms:

SHA-256 produces the same hash regardless of the tool or operating system used. A file hashed with sha256sum on Linux, shasum on macOS, and this browser tool will all produce identical results. The hash depends solely on the file's byte content, not on the filename, file system metadata, or the hashing tool used.

Use Case

File hashing is essential for build systems, content-addressable storage, forensic evidence integrity, and verifying that transferred files match their originals.

Try It — Hash Generator

Open full tool