【问题标题】:Password hash and salt using bcrypt in React Native (Expo)在 React Native (Expo) 中使用 bcrypt 的密码哈希和盐
【发布时间】:2023-01-08 05:19:14
【问题描述】:

我有一个 React Native Expo 应用程序,我想在其中实现存储用户密码哈希和盐的身份验证。 为此,我正在尝试使用 bcryptjs 库。 这是我用来生成盐的代码:

var salt = bcrypt.genSaltSync(10);

但是,我在上述行收到此消息的错误:

Error: Requiring module "node_modules\react-native-crypto\index.js", which threw an exception: TypeError: null is not an object (evaluating 'RNRandomBytes.seed')
at node_modules\react-native\Libraries\LogBox\LogBox.js:149:8 in registerError
at node_modules\react-native\Libraries\LogBox\LogBox.js:60:8 in errorImpl
at node_modules\react-native\Libraries\LogBox\LogBox.js:34:4 in console.error
at node_modules\expo\build\environment\react-native-logs.fx.js:27:4 in error
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:104:6 in reportException
at node_modules\react-native\Libraries\Core\ExceptionsManager.js:172:19 in handleException
at node_modules\react-native\Libraries\Core\setUpErrorHandling.js:24:6 in handleError
at node_modules\expo-error-recovery\build\ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
at node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
at node_modules\regenerator-runtime\runtime.js:294:29 in invoke
at node_modules\regenerator-runtime\runtime.js:63:36 in tryCatch
at node_modules\regenerator-runtime\runtime.js:155:27 in invoke
at node_modules\regenerator-runtime\runtime.js:165:18 in PromiseImpl.resolve.then$argument_0
at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:123:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:177:14 in _callImmediatesPass
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:437:30 in callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:388:6 in __callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:132:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:365:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:131:4 in flushedQueue

这是我的 package.json 内容:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "postinstall": "node_modules/.bin/rn-nodeify --install crypto,assert,url,stream,events --hack"
  },
  "dependencies": {
    "@react-navigation/native": "^5.9.8",
    "@react-navigation/stack": "^5.14.9",
    "@tradle/react-native-http": "^2.0.0",
    "assert": "^1.1.1",
    "bcryptjs": "^2.4.3",
    "browserify-zlib": "^0.1.4",
    "console-browserify": "^1.1.0",
    "constants-browserify": "^1.0.0",
    "convert-array-to-csv": "^2.0.0",
    "dns.js": "^1.0.1",
    "domain-browser": "^1.1.1",
    "dropbox": "^10.23.0",
    "events": "^1.0.0",
    "expo": "~43.0.0",
    "expo-crypto": "~10.0.3",
    "expo-file-system": "^13.0.3",
    "expo-media-library": "~13.0.3",
    "expo-print": "~11.0.4",
    "expo-random": "~12.0.1",
    "expo-status-bar": "~1.1.0",
    "formik": "^2.1.4",
    "https-browserify": "^0.0.1",
    "patch-package": "^6.4.7",
    "path-browserify": "^0.0.0",
    "process": "^0.11.0",
    "punycode": "^1.2.4",
    "querystring-es3": "^0.2.1",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-crypto": "^2.2.0",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-level-fs": "^3.0.0",
    "react-native-os": "^1.0.1",
    "react-native-randombytes": "^3.6.1",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.8.0",
    "react-native-tcp": "^3.2.1",
    "react-native-udp": "^2.1.0",
    "react-native-web": "0.17.1",
    "readable-stream": "^1.0.33",
    "stream-browserify": "^1.0.0",
    "string_decoder": "^0.10.31",
    "timers-browserify": "^1.0.1",
    "tty-browserify": "^0.0.0",
    "url": "^0.10.3",
    "util": "^0.10.4",
    "vm-browserify": "^0.0.4",
    "yup": "^0.32.11"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "rn-nodeify": "^10.3.0"
  },
  "private": true,
  "react-native": {
    "zlib": "browserify-zlib",
    "console": "console-browserify",
    "constants": "constants-browserify",
    "crypto": "react-native-crypto",
    "dns": "dns.js",
    "net": "react-native-tcp",
    "domain": "domain-browser",
    "http": "@tradle/react-native-http",
    "https": "https-browserify",
    "os": "react-native-os",
    "path": "path-browserify",
    "querystring": "querystring-es3",
    "fs": "react-native-level-fs",
    "_stream_transform": "readable-stream/transform",
    "_stream_readable": "readable-stream/readable",
    "_stream_writable": "readable-stream/writable",
    "_stream_duplex": "readable-stream/duplex",
    "_stream_passthrough": "readable-stream/passthrough",
    "dgram": "react-native-udp",
    "stream": "stream-browserify",
    "timers": "timers-browserify",
    "tty": "tty-browserify",
    "vm": "vm-browserify",
    "tls": false
  },
  "browser": {
    "zlib": "browserify-zlib",
    "console": "console-browserify",
    "constants": "constants-browserify",
    "crypto": "react-native-crypto",
    "dns": "dns.js",
    "net": "react-native-tcp",
    "domain": "domain-browser",
    "http": "@tradle/react-native-http",
    "https": "https-browserify",
    "os": "react-native-os",
    "path": "path-browserify",
    "querystring": "querystring-es3",
    "fs": "react-native-level-fs",
    "_stream_transform": "readable-stream/transform",
    "_stream_readable": "readable-stream/readable",
    "_stream_writable": "readable-stream/writable",
    "_stream_duplex": "readable-stream/duplex",
    "_stream_passthrough": "readable-stream/passthrough",
    "dgram": "react-native-udp",
    "stream": "stream-browserify",
    "timers": "timers-browserify",
    "tty": "tty-browserify",
    "vm": "vm-browserify",
    "tls": false
  }
}

谁能帮我解决这个问题?我究竟做错了什么? 提前谢谢了。

【问题讨论】:

  • 您如何测试该应用程序?您需要一个本地环境才能通过react-native-crypto(带有适当的链接)运行代码。您可以使用 EAS 运行开发配置文件:docs.expo.dev/build/introduction
  • @Nelloverflow 我正在 Android 虚拟设备上运行它。你能解释一下为什么我在使用 bcryptjs 时需要 react-native-crypto 吗?
  • 一些 NPM 包依赖于 Node Native 模块。试试这个专为docs.expo.dev/versions/latest/sdk/crypto设计的套餐
  • 如果他需要腌制,那个包裹可能不适合他的需要。试试这个是否适用于当前版本的 Expo:github.com/andreshsingh/bcrypt-react-native 如果您使用的纯 JS 版本不是,那么您很可能在任何情况下都需要适合 React-Native/Expo 的本地实现的东西举止得体
  • 我不打算深入研究这样一个事实,即从你描述事物的方式来看,无论如何在事物的客户端散列东西是不正确的,但归根结底,这是你的选择

标签: reactjs react-native expo bcrypt


【解决方案1】:

试试这个:

import BcryptReactNative from 'bcrypt-react-native';

// ...
try{

const salt = await BcryptReactNative.getSalt(10);
const hash = await BcryptReactNative.hash(salt, 'password');
const isSame = await BcryptReactNative.compareSync('password', hash);
} catch(e) {
  console.log({ e })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-24
    • 2011-01-09
    • 2014-06-09
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多