Skip to content

Instantly share code, notes, and snippets.

@eungjun-yi eungjun-yi/sha1sum.html
Last active Dec 21, 2017

Embed
What would you like to do?
<html>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script>
<script src='http://crypto-js.googlecode.com/svn/tags/3.1.2/build/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>
@jamesmontalvo3

This comment has been minimized.

Copy link

jamesmontalvo3 commented Feb 1, 2017

I had to modify your WordArray.create call to add byte length:

var bytes = CryptoJS.lib.WordArray.create( e.target.result, e.target.result.byteLength );

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.

@jamesmontalvo3

This comment has been minimized.

Copy link

jamesmontalvo3 commented Feb 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.