Calcuate MD5 hash of a large file using javascript -


how upload 500mb file , md5 hash cryptojs?

here code:

$('#upload-file').change(function(){     var reader = new filereader();     reader.addeventlistener('load',function () {         var hash = cryptojs.md5(cryptojs.enc.latin1.parse(this.result));         window.md5 = hash.tostring(cryptojs.enc.hex);     });      reader.readasbinarystring(this.files[0]); }); 

if file under 200mb, works. bigger, this.result empty "".

i've tried:

filereader api on big files

javascript filereader - parsing long file in chunks

and got work , console complaining .join("")

http://dojo4.com/blog/processing-huge-files-with-an-html5-file-input

cryptojs has progressive api hash digests. rest taken form alediaferia's answer slight modifications.

function process() {    getmd5(      document.getelementbyid("my-file-input").files[0],      prog => console.log("progress: " + prog)    ).then(      res => console.log(res),      err => console.error(err)    );  }    function readchunked(file, chunkcallback, endcallback) {    var filesize   = file.size;    var chunksize  = 4 * 1024 * 1024; // 4mb    var offset     = 0;        var reader = new filereader();    reader.onload = function() {      if (reader.error) {        endcallback(reader.error || {});        return;      }      offset += reader.result.length;      // callback handling read chunk      // todo: handle errors      chunkcallback(reader.result, offset, filesize);       if (offset >= filesize) {        endcallback(null);        return;      }      readnext();    };      reader.onerror = function(err) {      endcallback(err || {});    };      function readnext() {      var fileslice = file.slice(offset, offset + chunksize);      reader.readasbinarystring(fileslice);    }    readnext();  }    function getmd5(blob, cbprogress) {    return new promise((resolve, reject) => {      var md5 = cryptojs.algo.md5.create();      readchunked(blob, (chunk, offs, total) => {        md5.update(cryptojs.enc.latin1.parse(chunk));        if (cbprogress) {          cbprogress(offs / total);        }      }, err => {        if (err) {          reject(err);        } else {          // todo: handle errors          var hash = md5.finalize();          var hashhex = hash.tostring(cryptojs.enc.hex);          resolve(hashhex);        }      });    });  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/md5.js"></script>  <input id="my-file-input" type="file">  <button onclick="process()">process</button>


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -