Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use window.crypto in nodejs code

Tags:

I am trying to use the window.crypto.getRandomValues method in a nodejs script. From my understanding there is no window element when I run a simple code like this in node:

var array = new Uint32Array(10); window.crypto.getRandomValues(array); 

Which is why I get this error:

ReferenceError: window is not defined 

How can I use this method in my code?

Thanks

like image 961
Spearfisher Avatar asked Sep 08 '14 13:09

Spearfisher


People also ask

Is crypto built in Node JS?

It includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. crypto is built into Node. js, so it doesn't require rigorous implementation process and configurations.

How do I hash in Node JS?

The hash. digest( ) method is an inbuilt function of the crypto module's Hash class. This is used to create the digest of the data which is passed when creating the hash. For example, when we create a hash we first create an instance of Hash using crypto.


1 Answers

You can use the built-in crypto module instead. It provides both a crypto.randomBytes() as well as a crypto.pseudoRandomBytes().

However it should be noted that these methods give you a Buffer object, you cannot pass in a Uint32Array or similar, so the API is a bit different.

like image 145
mscdex Avatar answered Sep 18 '22 13:09

mscdex