Browser File Integrity Checker with JavaScript
In this article
This guide shows you how to build a browser-based file integrity checker using JavaScript and the Web Crypto API. It lets users select a file, choose a hash algorithm (SHA-256, SHA-384, or SHA-512), and instantly compute the file's hash — all without uploading the file to any server. You'll also add a field to compare against an expected hash, which is useful for verifying downloads or checking if a file has been altered.
What You'll Need
- A modern web browser (Chrome, Firefox, Edge, or Safari) that supports the Web Crypto API (available since 2017).
- A basic text editor for writing HTML, CSS, and JavaScript.
- No server or build tools are required — everything runs client-side.
Step-by-Step
- Create the HTML structure. Start with a simple page containing a file input, a select dropdown for hash algorithms, a button to compute the hash, an output area for the result, and an optional input for an expected hash to compare against.
- Add the CSS for a clean layout. Style the page minimally so it's usable on desktop and mobile. Use flexbox or grid to center the container and space out the controls.
- Set up the JavaScript file. Create a
script.jsfile and link it at the end of the body. Inside, define variables that reference the DOM elements: file input, algorithm select, compute button, output div, and expected hash input. - Add an event listener to the compute button. When clicked, it should get the selected file, check that one exists, and then call a function to compute the hash.
- Implement the hash computation function. Use
crypto.subtle.digest(). This function takes the algorithm name (e.g., 'SHA-256') and an ArrayBuffer of the file data. You'll need to read the file as an ArrayBuffer usingFileReaderorfile.arrayBuffer()(the latter is simpler and widely supported). - Convert the hash to a hex string. The digest returns an ArrayBuffer of bytes. Convert it to a hex string by iterating over each byte and using
toString(16).padStart(2, '0'). Display this in the output area. - Add the comparison feature. If the user enters an expected hash (in hex), compare it with the computed hash (case-insensitive). Show a message like "Match" or "Mismatch" with appropriate styling (green/red).
- Handle errors gracefully. Wrap the hash computation in a try-catch block. If the file is too large or the algorithm is unsupported, show a user-friendly error message.
- Test with a known file. Use a small text file and compare the hash with a tool like
sha256sumon Linux/macOS or PowerShell'sGet-FileHashon Windows. This ensures your implementation is correct. - Polish the UI. Add a loading state (e.g., disable the button and show "Computing…") while the hash is being calculated, especially for large files. Re-enable the button when done.
Common Mistakes
- Forgetting to read the file as ArrayBuffer. The Web Crypto API expects an ArrayBuffer, not a string. If you pass a string, it will throw a TypeError.
- Using the wrong algorithm name. The names must be exactly 'SHA-256', 'SHA-384', or 'SHA-512'. Lowercase or hyphen variations will fail.
- Not handling large files. Reading a multi-gigabyte file into memory can crash the browser. Consider adding a file size limit (e.g., 500 MB) and warning the user.
- Comparing hashes with different cases. Always normalize both to lowercase (or uppercase) before comparison to avoid false mismatches.
- Assuming the crypto.subtle is available. It only works in secure contexts (HTTPS or localhost). If you open the file directly (file://), it will be undefined.
Tips
- Use
file.arrayBuffer()instead of FileReader. It's cleaner and returns a Promise, making your code more readable. - Display the hash in chunks. For long hashes, break them into groups of 4 or 8 characters for easier reading and comparison.
- Add a copy-to-clipboard button. This is a small UX win that users appreciate.
- Support drag-and-drop. Allow users to drop a file onto the page instead of using the file picker. This is a nice enhancement.
- Cache the last computed hash. If the user changes the algorithm without changing the file, you can reuse the file data instead of re-reading it.
Troubleshooting
- “crypto.subtle is undefined” — You're likely opening the page via
file://. Serve it over HTTP (e.g., using a simple local server likepython -m http.server) or use localhost. Alternatively, test on a hosted page. - Hash result is always empty. Check that you're waiting for the Promise to resolve. Use
awaitinside an async function, or use.then(). Also verify that the file is actually selected. - Large files freeze the browser. The hash computation is synchronous after reading the file, and reading a huge file can block the UI. Add a file size limit or show a progress indicator (though true progress is hard with the current API).
- Hash doesn't match external tools. Ensure you're converting to hex correctly. A common bug is missing the
padStart(2, '0')for bytes under 16, which drops leading zeros. - Algorithm dropdown doesn't work. Make sure you're reading the value from the select element inside the event listener, not at page load.
Frequently Asked Questions
Is this secure enough for verifying file integrity?
Yes, SHA-256, SHA-384, and SHA-512 are cryptographically secure hash functions. However, note that hashing only verifies that the file hasn't changed since the hash was computed; it doesn't prove authenticity. For authenticity, you'd need a digital signature.
Can I use this to check a file's checksum from a download?
Absolutely. If a website provides a SHA-256 checksum, you can download the file, open this tool, select the file, enter the expected hash, and see if it matches.
Does this work offline?
Yes, once the page is loaded, all computation happens locally in your browser. No internet connection is needed.
What is the maximum file size I can process?
It depends on your browser's memory limits. Typically, files up to a few hundred megabytes work fine. For larger files, you might need to use streaming techniques, which are more complex and not covered here.
Can I hash multiple files at once?
This guide covers a single file. To support multiple files, you'd need to loop over the fileInput.files array and compute each hash separately.
Why do I get a different hash than a command-line tool?
Ensure you're using the same algorithm and that the file hasn't been modified (e.g., line endings on Windows). Also, check that you're comparing the full hex string, not a truncated version.