-
-
Save fzn0x/9e0a4eeaf69f11cec5dafa2e66b42df2 to your computer and use it in GitHub Desktop.
Compute sha1 checksum of a file. Inspired by http://stackoverflow.com/questions/18586839/read-file-stream-using-javascript-in-web-browser?answertab=votes#tab-top
This file contains 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
<html> | |
<!-- You may need to download them from https://github.com/brix/crypto-js/tree/release-3.1.2/build --> | |
<script src="rollups/sha1.js"></script> | |
<script src='components/lib-typedarrays-min.js'></script> | |
<body> | |
<script> | |
function sha1sum() { | |
var oFile = document.getElementById('uploadFile').files[0]; | |
var sha1 = CryptoJS.algo.SHA1.create(); | |
var read = 0; | |
var unit = 1024 * 1024; | |
var blob; | |
var reader = new FileReader(); | |
reader.readAsArrayBuffer(oFile.slice(read, read + unit)); | |
reader.onload = function(e) { | |
var bytes = CryptoJS.lib.WordArray.create(e.target.result); | |
sha1.update(bytes); | |
read += unit; | |
if (read < oFile.size) { | |
blob = oFile.slice(read, read + unit); | |
reader.readAsArrayBuffer(blob); | |
} else { | |
var hash = sha1.finalize(); | |
console.log(hash.toString(CryptoJS.enc.Hex)); // print the result | |
} | |
} | |
} | |
</script> | |
<form name="uploadForm"> | |
<input id="uploadFile" type="file" onchange="sha1sum();" /> | |
<input type="submit" value="Send" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment