Generate An MD5 Hash of An Image Using HTML5 / JavaScript

泪湿孤枕 提交于 2020-01-02 06:45:08

问题


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]);

回答1:


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>


来源:https://stackoverflow.com/questions/31236114/generate-an-md5-hash-of-an-image-using-html5-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!