How to compute SHA hash of local file in Javascript using SubtleCrypto API

The following example uses the SubtleCrypto API to compute the SHA-256 hash of a file that is selected in a file input. The checksum is computed entirely on the client side, the file does not need to be uploaded to a server at all.

This code has been verified to generate the same checksum as if running sha256sum on the command line.

Full example

You can download this file, save it asĀ index.html and open it in the browser. Then select a file and check the develper

    <input type="file" id="myfile" onchange="onMyfileChange(this)" />

    <script src="" type="text/javascript"></script>
    <script type="text/javascript">
        function onMyfileChange(fileInput) {
            if(fileInput.files[0] == undefined) {
                return ;

            var filename = fileInput.files[0].name;
            // var filesize = fileInput.files[0].size;
            var reader = new FileReader();
            reader.onload = function(ev) {
                console.log("File", filename, ":");
                crypto.subtle.digest('SHA-256', => {
                    // Convert hex to hash, see
                    const hashArray = Array.from(new Uint8Array(hashBuffer));
                    const hashHex = => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
                }).catch(ex => console.error(ex));
            reader.onerror = function(err) {
                console.error("Failed to read file", err);