JavaScript: SHA-256 Implementation

This weekend I decided to experiment with using JavaScript to implement client-side processing of files using a web browser. To make it interesting, I wanted to see if it was possible to implement a cryptographically strong hash function: SHA-256.

This exercise gave me exposure to Web Workers and the Stream API. I encountered compatibility challenges. Google Chrome does not respond to UI updates when using setInterval() or setTimeout() while the browser is busy, but its user interface can be updated when processing messages from web workers. Mozilla Firefox does not support passing {type: ‘module’} to the Worker() constructor; ECMAScript modules are unsupported. Firefox reports a syntax error when trying to import modules from the Web Worker’s source file. This forced me to add Worker code to the source code file containing the JavaScript SHA-256 implementation.

The following checksum generator implements SHA-256 as defined by FIPS PUB 180-4. Files are not sent to the server for processing. The cost of transferring data would be too much. Files are processed locally on the web browser running on the user’s computer.

SHA-256 Checksum Generator

Input File:
Source Code: sha256.js, sha256main.js

Leave a Reply