我有一个反应本机博览会应用程序,我想在其中实现存储用户密码哈希和盐的身份验证。 为此,我尝试使用 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
}
}
有人可以帮我解决这个问题吗?我做错了什么? 非常感谢。
尝试一下:
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 })
}