由于某种原因,我试图使用的函数显然不是函数。
Welcome to Node.js v14.15.1.
Type ".help" for more information.
> const crypto = require("crypto");
undefined
> x = crypto.randomBytes(32).toString("hex")
Uncaught TypeError: crypto.randomBytes is not a function
有什么我不明白的吗?
发布于 2022-02-14 00:59:17
似乎getRandomBytes()函数被删除了。我读了一些免责声明,说它不是很安全。
https://www.npmjs.com/package/crypto充斥着反对意见的信息,所以在https://stackoverflow.com/a/8856177/828184的领导下,大多数人都是这样的,对我来说,这似乎不再是最先进的了。
之前,我可以简单地使用(像你一样,但不再在包更新后)
import crypto from "crypto";
const token = crypto.randomBytes(64).toString('hex');
但是现在密码只有getRandomValues(),我认为它不是替代品。
只回答nr 3也有很多,但没有那么多的向上给我一个工作版本的https://stackoverflow.com/a/25690754/828184。所以也可以试试:
import { nanoid } from "nanoid";
const token = nanoid(64); //instead of crypto.randomBytes(64).toString('hex')
如果有效的话就在那里投票吧,因为。
发布于 2022-03-15 12:15:09
如果您试图创建一个token
。
您只需在nodejs中键入以下命令:
crypto.randomBytes(64).toString('hex');
发布于 2022-11-16 06:59:33
我在命令行中遇到了同样的错误:Uncaught TypeError: crypto.randomBytes is not a function
这对我没有用:
$ node
> require("crypto")
> crypto.randomBytes(32).toString("hex")
密码和randomBytes必须在同一个命令中调用:
$ node
> require('crypto').randomBytes(32).toString('hex')
输出如下所示:
'7a3161b8c92dbf26f0717e89edd27bf10094d2f5cc0f4f2d70d08f463f2881db‘
经过一番搜索,我终于在这里找到了解决方案:https://massimilianomarini.com/2020/04/random-string/
https://stackoverflow.com/questions/70566188
复制相似问题