javascript - Browser freezes (OOM?) when computing a large file MD5 using JS Crypto library -
i using webpack, compiling bundled js file.
the problem
i have worker offloading hashing work to. pass file , filesize it. did not use worker. however, when chrome reacted badly hashing large file, thought main thread being blocked hashing mechanism. false assumption.
the code works correctly small files. however, large files, once reaching part final hash generated, chrome shows error:
firefox bit more helpful , shows message:
error: uncaught, unspecified "error" event. (out of memory)
however, piping of data should alleviate issue. filereaderstream
reads data in chunks of 1 mb.
the code
import crypto 'crypto' import filereaderstream 'filereader-stream' import concat 'concat-stream' var progress = require('progress-stream'); self.onmessage = (event) => { switch (event.data.topic) { case 'hash': { var file = event.data.file; var filesize = event.data.filesize; let p1 = progress({ length: filesize, time: 100 /* ms */ }); let p2 = progress({ length: filesize, time: 100 /* ms */ }); p1.on('progress', function(progress) { console.log('p1', progress); }); p2.on('progress', function(progress) { console.log('p2', progress); }); let md5 = crypto.createhash('md5'); console.log("start hash"); var reader = filereaderstream(file); reader.pipe(p1).pipe(md5).pipe(p2).pipe(concat((data) => { console.log("done hash"); console.log(data); })); break; } } }
small file example (5,248 kb)
large file example (643 mb)
additional information
screenshot of memory usage. takes 3 gb in few seconds.
so worth using different library if 1 poorly implemented regards memory management.
this javascript library implemented stanford - https://bitwiseshiftleft.github.io/sjcl/
you may want consider using more secure hashing algorithm md5 due it's vulnerability collisions via birthday attack.
Comments
Post a Comment