Running window.crypto.getRandomValues ​​() from a web worker

I'm here. Is there a way to do this, knowing that web workers cannot access the window object? Please, help!

+4
source share
2 answers

There is no way to do this, because the Crypto class and methods are "native code", that is, they are implemented at a lower level in the browser, so we can’t extract the code and move it to another place. I tried using the Chrome Transferrable Objects . which is removed from the current context and passed to the employee, but this causes an error. I do not think that crypto-api should be associated with the DOM (WebWorkers will not touch the DOM, since it is not thread safe), but I have never implemented cryptography before. In Node, we have async api, so for me, at least, it looks like it should be thread safe.

http://cl.ly/image/0r0P3m3D2h07 <- Check window.crypto

http://cl.ly/image/0G1G0F1Y0d3Z <- Sample code for transferring cryptographic code to the WebWorker browser.

I created a WebWorker script to make sure that maybe it’s just possible that Chrome and Firefox had different implementations, and unfortunately I was wrong.

I sent an email to the authors of the W3C Web Crypto API Proposal , and you should as well. At the moment, the project has several open questions, so it seems that it can change. Having either an asynchronous API or a crypto API available in Web Workers seems perfectly reasonable to me. It seems to be bad to block the main event loop.

+3
source

I know this is an old question, but I stumbled and everything changed. Most browsers now support crypto in webmasters.

In web managers, you can access self , which does not contain all the properties of the "window" (especially nothing related to dom), but contains API methods such as cryptographic functions.

Therefore, you can simply access self.crypto.getRandomValues() from the website.

I made a fiddle as an example: http://jsfiddle.net/jbrosi/yj17gomk/

However, ignore the fact that calls to webworker and back also have a small impact on performance, and the most expensive crypto functions (e.g. crypto.subtle.encrypt are asynchronous and therefore should not block your mainthread at all.

0
source

Source: https://habr.com/ru/post/1441175/


All Articles