Itβs pretty easy to calculate the MD5 hash using the MD5 function from CryptoJS and the HTML5 FileReader API . The following code snippet shows how you can read binary data and calculate the MD5 hash from an image that has been dragged into your browser:
var holder = document.getElementById('holder'); holder.ondragover = function() { return false; }; holder.ondragend = function() { return false; }; holder.ondrop = function(event) { event.preventDefault(); var file = event.dataTransfer.files[0]; var reader = new FileReader(); reader.onload = function(event) { var binary = event.target.result; var md5 = CryptoJS.MD5(binary).toString() console.log(md5); }; reader.readAsBinaryString(file); };
I recommend adding CSS to see the drag area:
#holder { border: 10px dashed #ccc; width: 300px; height: 300px; } #holder.hover { border: 10px dashed #333; }
More information about the drag and drop function can be found here: File API and FileReader
I tested the sample in Google Chrome version 32.
Benny Neugebauer Jan 30 '14 at 1:36 on 2014-01-30 13:36
source share