在nodejs代码中使用window.crypto [英] Use window.crypto in nodejs code

查看:254
本文介绍了在nodejs代码中使用window.crypto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在nodejs脚本中使用 window.crypto.getRandomValues 方法。根据我的理解,当我在节点中运行这样的简单代码时,没有窗口元素:

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?

谢谢

推荐答案

你可以使用内置的 crypto 模块而不是。它同时提供 crypto.randomBytes() 以及 加密。 pseudoRandomBytes()

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

但是应该注意这些方法给你一个Buffer对象,你不能传入Uint32Array或类似的,所以API有点不同。

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.

这篇关于在nodejs代码中使用window.crypto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆