Use window.crypto in nodejs code
Asked Answered
R

7

36

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

Ritenuto answered 8/9, 2014 at 13:37 Comment(2)
What about crypto.subtle.generateKey(...)?Complimentary
In "window" is only used in web development Javascript, not node.js. Use globalThis to replace window.Pinnacle
U
21

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.

Unmerciful answered 8/9, 2014 at 13:40 Comment(3)
ok thanks. How can I turn the buffer object returned as a simple 256-bit number?Ritenuto
You'd have to use some javascript big number/integer library to convert the bytes to a number that large.Unmerciful
Note that as of version 7.10.0 of Node, there is a crypto.randomFillSync() function in NodeJS which allows you to pass a TypedArray.Kearns
C
23
const crypto = require('crypto').webcrypto;

let a = new Uint8Array(24);
console.log(crypto.getRandomValues(a));

This works almost exactly like the one in the browser, by adding webcrypto to the end of requrie('crypto');.

Civism answered 18/3, 2021 at 14:9 Comment(3)
Should note that crypto.webcrypto is only available in Node.js 15.0.0+. LTS versions do not yet support it.Snafu
Node 16 is LTS now and this makes it a great answer!Hostess
Typescript makes using this harder than expected github.com/denoland/node_deno_shims/issues/56 ... to bypass I used const crypto = webcrypto as anyCantu
U
21

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.

Unmerciful answered 8/9, 2014 at 13:40 Comment(3)
ok thanks. How can I turn the buffer object returned as a simple 256-bit number?Ritenuto
You'd have to use some javascript big number/integer library to convert the bytes to a number that large.Unmerciful
Note that as of version 7.10.0 of Node, there is a crypto.randomFillSync() function in NodeJS which allows you to pass a TypedArray.Kearns
C
11

You can use this module which is the same as the window element: get-random-values

Install it:

npm install get-random-values --save

Use it:

var getRandomValues = require('get-random-values');

var array = new Uint32Array(10);
getRandomValues(array);
Coaler answered 16/3, 2016 at 1:21 Comment(3)
This isn't the same as the window element, this use nodejs cryptoMarenmarena
TypeError: expected Uint8ArrayAntigorite
This should be the correct answer, with a simple addition, this can behave the same as the browser version.Middelburg
C
2

Here is how to use it in Node 16 with TypeScript. I'm hijacking the web types and overriding the @types/node type, which are missing webcrypto.

import { webcrypto } from 'crypto'
const crypto = webcrypto as unknown as Crypto
const random = crypto.getRandomValues(new Uint8Array(24))

This sandbox will work in Node 16, but stackblitz won't release node 16 for another couple months. https://stackblitz.com/edit/koa-starter-wychx9?file=package.json

Issue: github.com/denoland/node_deno_shims/issues/56

Cantu answered 2/2, 2022 at 18:11 Comment(0)
A
2

As of Node.js v19.0.0 (noted in this changelog: https://github.com/nodejs/node/releases/tag/v19.0.0) globalThis.crypto in Node.js is now the same as webcrypto imported from 'crypto':

So you can now do this and get the same (random) result in Node.js as in the browser:

globalThis.crypto.getRandomValues

Note the usage of globalThis rather than window. See docs here if needed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis

Avaavadavat answered 9/10, 2023 at 12:20 Comment(0)
C
0

I had this problem too, I solved it this way

import * as crypto from 'node:crypto'

export function randomChar() {
  return crypto.webcrypto.getRandomValues(new BigUint64Array(1))[0].toString(36)
}

Reference: How to use getRandomValues() in nodejs?

Circumlocution answered 2/2, 2023 at 19:24 Comment(0)
O
0

In Node.js 19 you can just use it (without window.)

const array = new Uint32Array(10);
crypto.getRandomValues(array);
Olcott answered 6/4, 2023 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.