tweetnacl 在 React Native 中不返回 PRNG 错误

Jer*_*rry 2 react-native tweet-nacl

我在 React Native 中使用 nacl

我只是在下面写下代码。

它在 randombytes 变量中不返回 PRNG 错误。

  const test = nacl.box.keyPair();
Run Code Online (Sandbox Code Playgroud)

这也是我的 package.json 。

{
  "dependencies": {
    "@react-navigation/material-bottom-tabs": "^6.2.15",
    "@react-navigation/native": "^6.1.6",
    "@react-navigation/native-stack": "^6.9.12",
    "bs58": "^5.0.0",
    "react": "18.2.0",
    "react-native": "0.71.3",
    "react-native-get-random-values": "^1.8.0",
    "react-native-paper": "^5.4.1",
    "react-native-safe-area-context": "^4.5.0",
    "react-native-screens": "^3.20.0",
    "react-native-webview": "^11.26.1",
    "tweetnacl": "^1.0.3"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native-community/eslint-config": "^3.2.0",
    "@tsconfig/react-native": "^2.0.2",
    "@types/jest": "^29.2.1",
    "@types/react": "^18.0.24",
    "@types/react-test-renderer": "^18.0.0",
    "metro-react-native-babel-preset": "0.73.7",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  }
}

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 5

我以前也遇到过同样的问题。在研究过程中我发现内部nacl使用nacl.randomBytes()。但是,实际实现会根据您使用的平台而有所不同。因为node它调用了crypto.randomBytes()ReactNative 环境中未定义的 。

要使用naclReactNative,您需要自己提供定义。您可以通过安装react-native-get-random-values软件包并添加

import 'react-native-get-random-values'
Run Code Online (Sandbox Code Playgroud)

index.js

这将提供nacl的定义crypto.randomBytes()