Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running window.crypto.getRandomValues() from inside a Web Worker

I'm at my wit's end here. Is there any way to do this, knowing that web workers can't access the window object? Please help!

like image 790
kaepora Avatar asked Oct 22 '12 02:10

kaepora


People also ask

What is window crypto getRandomValues?

The Crypto. getRandomValues() method lets you get cryptographically strong random values. The array given as the parameter is filled with random numbers (random in its cryptographic meaning).

What is window Crypto?

The Window. crypto property returns the crypto object associated with the global object. This is a read-only property. This object allows web pages to access certain cryptographic related services.


1 Answers

I know this is an old question but i stumbled accross and things changed. Most browsers do support crypto in webworkers now.

In webworkers you can access self which does not contain all of the "window" properties (especially nothing dom-related), but holds API methods like crypto functions.

Therefore you can simply access self.crypto.getRandomValues() from within webworker.

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

However please not that calls to webworker and back are also having a small performance impact and most expensive crypto functions (like crypto.subtle.encrypt are async and should therefore not block your mainthread at all.

like image 132
jbrosi Avatar answered Sep 18 '22 01:09

jbrosi