Comment patch / shim crypto.getRandomValues ​​pour React Native

Je porte certains packages créés pour NodeJS pour React Native en utilisant ReactNativify afin de réécrire les dépendances des objects de l’API Node dans leurs équivalents browserify.

L’un d’eux est la crypto . Dans transformer.js (ou .babelrc ), j’ai:

 // The following plugin will rewrite imports. Reimplementations of node // libraries such as `assert`, `buffer`, etc. will be picked up // automatically by the React Native packager. All other built-in node // libraries get rewritten to their browserify counterpart. [require('babel-plugin-rewrite-require'), { aliases: { crypto: 'crypto-browserify', // ... }, throwForNonSsortingngLiteral: true, }], 

Dans ReactNativify global.js il y a ce code (que j’ai exclu, car il n’est pas destiné à la production):

 // Don't do this in production. You're going to want to patch in // https://github.com/mvayngrib/react-native-randombytes or similar. global.crypto = { getRandomValues(byteArray) { for (let i = 0; i < byteArray.length; i++) { byteArray[i] = Math.floor(256 * Math.random()); } }, }; 

.

Ma première question : comment getRandomValues est- getRandomValues correctement corrigé pour la production?


Il existe une deuxième option, qui consiste à react-native-crypto (un clone de crypto-browserify )

Idéalement, je devrais pouvoir le faire dans transformer.js :

  aliases: { crypto: 'react-native-crypto', // instead of 'crypto-browserify' // ... }, 

Mais react-native-crypto utilise rn-nodeify au lieu de ReactNativify, qui génère un shim.js à importer dans index.android.js / index.ios.js avec un code similaire à celui-ci:

 if (require('./package.json').dependencies['react-native-crypto']) { const algos = require('browserify-sign/algos') if (!algos.sha256) { algos.sha256 = { "sign": "ecdsa", "hash": "sha256", "id": new Buffer("") } } if (typeof window === 'object') { const wCrypto = window.crypto = window.crypto || {} wCrypto.getRandomValues = wCrypto.getRandomValues || getRandomValues } const crypto = require('crypto') const randomBytes = crypto.randomBytes crypto.randomBytes = function (size, cb) { if (cb) return randomBytes.apply(crypto, arguments) const arr = new Buffer(size) getRandomValues(arr) return arr } crypto.getRandomValues = crypto.getRandomValues || getRandomValues function getRandomValues (arr) { // console.warn('WARNING: generating insecure psuedorandom number') for (var i = 0; i < arr.length; i++) { arr[i] = Math.random() * 256 | 0 } return arr } } 

Je ne sais pas si tout ce code de shim est nécessaire pour utiliser ReactNativify et ne peut pas trouver de bonnes sources, alors …

Ma deuxième question : comment utiliser react-native-crypto de la manière «appropriée» de ReactNativify?


J’ai créé des problèmes de github dans ReactNativify et un référentiel de crypto-natif-réactif:

  • Comment utiliser getRandomValues ​​en production?
  • Utiliser ReactNativify au lieu de rn-nodeify?

Versions:

 node 7.10.1 /usr/local/bin/node npm 4.2.0 /usr/local/bin/npm yarn 0.24.6 /usr/bin/yarn react-native-cli 2.0.1 app rn version 0.45.1 ignite 2.0.0 /usr/local/bin/ignite