Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate An MD5 Hash of An Image Using HTML5 / JavaScript

Using the HTML5 File API and any JavaScript crypto library, how can I generate an MD5 hash of the file?

To read the file:

var reader = new FileReader();

reader.onload = function(e) {
  var contents = e.target.result;
  // What goes here?
};

reader.readAsBinaryString(data.files[0]);
like image 719
Undistraction Avatar asked Jul 05 '15 23:07

Undistraction


1 Answers

This goes there:

var reader = new FileReader();

reader.onload = function(e) {
  var contents = e.target.result;
  // This goes here:
  var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(contents));
};

Be sure you include the CryptoJS library:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
like image 55
Maximillian Laumeister Avatar answered Oct 11 '22 01:10

Maximillian Laumeister