Last active
January 26, 2022 09:59
-
-
Save eungjun-yi/11282867 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> |
Sorry, in my version I didn't properly load http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/lib-typedarrays-min.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to modify your
WordArray.create
call to add byte length:Without this I got a
Uncaught RangeError: Invalid array length
error. However, this is still not computing the sha1 the same as on my backend.