简体   繁体   English

如何在 React Native 项目中使用 Node.JS 的 Crypto 内置模块?

[英]How can I use Crypto built-in module from Node.JS in React Native Project?

I'm working on a React Native project and I need to implement authentication, with an encrypted password.我正在做一个React Native project ,我需要使用加密密码实施身份验证。

I want to use library Bcrypt but I have an issue.我想使用library Bcrypt但遇到问题。 Bcrypt requires the Crypto module , which was a third-part library, but now it is a built-in module in Node.js . Bcrypt需要Crypto module ,这是一个第三方库,但现在它是Node.js中的一个内置模块。

When I try to use Bcrypt with const bcrypt = require('bcrypt') , it throws an error:当我尝试将 Bcrypt 与const bcrypt = require('bcrypt')一起使用时,它会引发错误:

Unable to resolve module 'crypto' from 'node_modules/bcrypt/...'无法从“node_modules/bcrypt/...”解析模块“crypto”

I am using:我在用:

  • Node.js v13.0.1 Node.js v13.0.1
  • NPM v6.12.0 NPM v6.12.0
  • react-native-cli v2.0.1反应本机-cli v2.0.1
  • react-native v0.61.3 react-native v0.61.3
  • react v16.9.0反应 v16.9.0

To manage this error, I went to Node.js documentation and tried the solution to know if the Crypto built-in module is available or not but it throws the same error.为了解决这个错误,我查阅了Node.js文档并尝试了解决方案来了解Crypto内置模块是否可用,但它抛出了同样的错误。

Maybe I don't understand clearly what is a built-in module, is that a module which is included into the Node.js installer , so I don't have any NPM install crypto to do?也许我不清楚什么是内置模块,是包含在 Node.js 安装程序中的模块,所以我没有任何 NPM 安装加密吗?

If it is the case, does anyone have an idea about why I can't use it in my project ?如果是这样,有没有人知道为什么我不能在我的项目中使用它

I also tried the rn-nodeify module , with react-native-bcrypt and react-native-crypto , but in each case some packages weren't reached, like stream , VM , fs , etc.我还尝试了rn-nodeify modulereact-native-bcryptreact-native-crypto ,但在每种情况下都没有到达一些包,比如streamVMfs等。

Do you encountered the same problem and solved it?你遇到过同样的问题并解决了吗?

Someone already answered that question here Bcrypt is not supported in Reactjs It's because bcrypt is written in C++.有人已经在这里回答了这个问题Bcrypt is not supported in Reactjs这是因为 bcrypt 是用 C++ 编写的。 But now they have bcryptjs, the JS version of bcrypt.但现在他们有了 bcryptjs,即 bcrypt 的 JS 版本。 You can download it here https://www.npmjs.com/package/bcryptjs .你可以在这里下载它https://www.npmjs.com/package/bcryptjs Note that bcryptjs is slower than the C++ version - bcrypt, because "it is written in pure JavaScript and thus slower (about 30%), effectively reducing the number of iterations that can be processed in an equal time span."请注意,bcryptjs 比 C++ 版本 - bcrypt 慢,因为“它是用纯 JavaScript 编写的,因此速度较慢(大约 30%),有效减少了可以在相等时间跨度内处理的迭代次数。”

I would request you to look into rn-nodeify library.我会要求您查看rn-nodeify库。 It enables you to use core modules and npm modules in your react-native app.它使您能够在 react-native 应用程序中使用核心模块和 npm 模块。

Look into this answer rn-nodeify installation and start from Step 2 .查看这个答案rn-nodeify 安装并从Step 2开始。

I hope i could help you.我希望我能帮助你。

https://github.com/margelo/react-native-quick-crypto I've used this lib the fix for endian.h is to rename it to <machine/endian.h> https://github.com/margelo/react-native-quick-crypto我已经使用了这个库 endian.h 的修复是将它重命名为 <machine/endian.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM