[英]Node.js crypto.randomBytes() is not a function
For some reason a function I am trying to use is, apparently, not a function .出于某种原因,我尝试使用的 function 显然不是 function 。
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
Documentation for randomBytes() . randomBytes()的文档。
Is there something I am not understanding?有什么我不明白的吗?
I ran into this same error in the command line: Uncaught TypeError: crypto.randomBytes is not a function
我在命令行中遇到了同样的错误:
Uncaught TypeError: crypto.randomBytes is not a function
This did NOT work for me:这对我不起作用:
$ node
> require("crypto")
> crypto.randomBytes(32).toString("hex")
Crypto and randomBytes have to be called in the same command:必须在同一命令中调用 Crypto 和 randomBytes:
$ node
> require('crypto').randomBytes(32).toString('hex')
The output is something like this: output 是这样的:
'7a3161b8c92dbf26f0717e89edd27bf10094d2f5cc0f4f2d70d08f463f2881db' '7a3161b8c92dbf26f0717e89edd27bf10094d2f5cc0f4f2d70d08f463f2881db'
After a good bit of searching, I finally found the solution here: https://massimilianomarini.com/2020/04/random-string/经过一番搜索,我终于在这里找到了解决方案: https://massimilianomarini.com/2020/04/random-string/
Seems getRandomBytes() function got removed.似乎 getRandomBytes() function 被删除了。 I read some disclaimers that it is not very secure.
我读了一些免责声明,说它不是很安全。
https://www.npmjs.com/package/crypto is clustured with deprecation messages so altough most upvotes here under https://stackoverflow.com/a/8856177/828184 it does no longer seem state of the art to me. https://www.npmjs.com/package/crypto is clustured with deprecation messages so altough most upvotes here under https://stackoverflow.com/a/8856177/828184 it does no longer seem state of the art to me.
Before I could simply use (like you but no longer after package updates)在我可以简单地使用之前(像你一样,但在 package 更新之后不再使用)
import crypto from "crypto";
const token = crypto.randomBytes(64).toString('hex');
But crypto now only has getRandomValues() and I think it is not a replacement.但是加密现在只有 getRandomValues(),我认为它不是替代品。
Only answer nr 3 with also a lot but not as many upvotes gave me a working version https://stackoverflow.com/a/25690754/828184 .只回答 nr 3 也有很多但没有那么多赞成票给了我一个工作版本https://stackoverflow.com/a/25690754/828184 。 So maybe also try:
所以也许也可以尝试:
import { nanoid } from "nanoid";
const token = nanoid(64); //instead of crypto.randomBytes(64).toString('hex')
And leave an upvote there if it works because.如果它有效,请在那里留下一个赞成票,因为。
If you attemped to create a token
.如果您尝试创建
token
。
You can just type the following command in your nodejs cli:您只需在 nodejs cli 中键入以下命令:
crypto.randomBytes(64).toString('hex');
I am facing the same issue, after that, I upgrade the node.js version 14 to 16, and Its works for me.我面临同样的问题,之后,我将 node.js 版本 14 升级到 16,它对我有用。 Please try to upgrade the node.js version.
请尝试升级node.js版本。
Ubuntu Install: https://computingforgeeks.com/how-to-install-node.js-on-ubuntu-debian/ Ubuntu 安装: https://computingforgeeks.com/how-to-install-node.js-on-ubuntu-debian/
Windows Install: https://nodejs.org/en/blog/release/v16.16.0/ Windows 安装: https://nodejs.org/en/blog/release/v16.16.0/
Mac Os Install: https://nodesource.com/blog/installing-nodejs-tutorial-mac-os-x/ Mac Os 安装: https://nodesource.com/blog/installing-nodejs-tutorial-mac-os-x/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.