srijs / rusha

High-performance pure-javascript SHA1 implementation suitable for large binary data, reaching up to half the native speed.
https://npmjs.org/rusha
MIT License
277 stars 32 forks source link

Feature/webworker filereader #43

Closed JamesKyburz closed 7 years ago

JamesKyburz commented 7 years ago

Thanks for your awesome module.

window.crypto doesn't allow progressive hashing but you do :)

This feature makes hashing large files in webworkers possible.

The current webworker support is great, but the problem is having to read the entire file in memory prior to posting data to the worker.

Using FileReader outside webworkers can work, but can also block the main thread as well as using a lot of memory.

With the support of using postMessage with a file argument, we can now do the following

function worker (file, cb) {
  const blob = new window.Blob([rusha], { type: 'text/javascript' })
  const url = window.URL.createObjectURL(blob)
  const webWorker = new window.Worker(url)
  webWorker.addEventListener('message', (e) => {
    cb(null, e.data.hash)
    window.URL.revokeObjectURL(url)
  })
  webWorker.postMessage({ id: file.name, file: file, BLOCK_SIZE: 10 * 1024 * 1024 })
}

I have added tests and verified them in safari.

Hashing 32.5 gb took about 2.5 minutes.

JamesKyburz commented 7 years ago

Any chance to take a look at this PR? Thanks.

JamesKyburz commented 7 years ago

@srijs Thanks for the feedback. Will update soon!

JamesKyburz commented 7 years ago

I have made the changes and rebased interactively.

The webworker tests passed too :)

JamesKyburz commented 7 years ago

How does the PR look? Let me know if there are any more changes needed.

JamesKyburz commented 7 years ago

Any chance this could be merged soon? Let me know if there are any issues.

JamesKyburz commented 7 years ago

Thanks!

srijs commented 7 years ago

Will cut a new release hopefully today.

On Thu, Mar 23, 2017, at 10:58 PM, James Kyburz wrote:

Thanks!

--

You are receiving this because you modified the open/close state.

Reply to this email directly or view it on GitHub:

https://github.com/srijs/rusha/pull/43#issuecomment-288873415

JamesKyburz commented 7 years ago

awesome!