Last active
October 29, 2024 11:23
-
-
Save JWally/4403220 to your computer and use it in GitHub Desktop.
Simple Method to Calculate the Hash of a file in Javascript using HTML5 API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Event Listener tied to the file input field | |
document.getElementById('attachment').addEventListener('change', eventHandler, false) | |
/** | |
*Description: Simple method to handle an event | |
*and fire off a function | |
**/ | |
function eventHandler(evt){ | |
var file = evt.target.files[0]; | |
fileHash( file, md5, function(x){ | |
console.log( x ); | |
}); | |
} | |
/** | |
*Description: The actual function to calculate the hash | |
*Arguments: | |
*file: a file from a file input load event | |
*hasher: hashing algorithm | |
*callback: function that does something with the hash | |
**/ | |
function fileHash( file, hasher, callback ){ | |
//Instantiate a reader | |
var reader = new FileReader(); | |
//What to do when we gets data? | |
reader.onload = function( e ){ | |
var hash = hasher(e.target.result); | |
callback( hash ); | |
} | |
reader.readAsBinaryString( file ); | |
} |
emersiljr
commented
Sep 10, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment