Use window.crypto in nodejs code Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Use window.crypto in nodejs code without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

Answer

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.

We are here to answer your question about Use window.crypto in nodejs code - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji