Skip to content

Instantly share code, notes, and snippets.

@arulprasad
Created November 30, 2015 20:11
Show Gist options
  • Save arulprasad/30c181d43576c4c99842 to your computer and use it in GitHub Desktop.
Save arulprasad/30c181d43576c4c99842 to your computer and use it in GitHub Desktop.
generate md5 for a file using CryptoJS
<html>
<head>
<title>Get MD5 for a file</title>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script type="text/javascript">
function onFileSelect(){
var reader = new FileReader();
reader.addEventListener('load',function () {
var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(this.result));
var md5 = hash.toString(CryptoJS.enc.Hex)
var filename = document.getElementById("input").value.split('/').pop().split('\\').pop();
var output = "MD5 (" + filename + ") = " + md5
console.log(output);
document.getElementById("md5").innerText = output
});
reader.readAsBinaryString(document.getElementById("input").files[0]);
}
</script>
</head>
<form>
<input type="file" id='input' onchange='onFileSelect();'>
</form>
<pre id="md5"></pre>
</html>
@zh-betina
Copy link

Thanks a lot! Struggled to get the right encoding!

@AlexandruFilipescu
Copy link

Thanks a lot, I used it for keccak hashing!!!

@jaques-tino
Copy link

Thanks man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment