node.js - Howto patch/shim crypto.getRandomValues for React Native -
i porting packages created nodejs react native using reactnativify rewrite node api object dependencies browserify equivalents.
one of them crypto
. in transformer.js
(or .babelrc
) have:
// following plugin rewrite imports. reimplementations of node // libraries such `assert`, `buffer`, etc. picked // automatically react native packager. other built-in node // libraries rewritten browserify counterpart. [require('babel-plugin-rewrite-require'), { aliases: { crypto: 'crypto-browserify', // ... }, throwfornonstringliteral: true, }],
in reactnativify global.js
there code (which excluded, because not meant production):
// don't in production. you're going want patch in // https://github.com/mvayngrib/react-native-randombytes or similar. global.crypto = { getrandomvalues(bytearray) { (let = 0; < bytearray.length; i++) { bytearray[i] = math.floor(256 * math.random()); } }, };
.
my first question: how
getrandomvalues
correctly patched production?
there second option, , using react-native-crypto
(a clone of crypto-browserify
)
ideally should able in transformer.js
:
aliases: { crypto: 'react-native-crypto', // instead of 'crypto-browserify' // ... },
but react-native-crypto
uses rn-nodeify instead of reactnativify, generates shim.js
imported in index.android.js
/ index.ios.js
code similar this:
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') (var = 0; < arr.length; i++) { arr[i] = math.random() * 256 | 0 } return arr } }
i don't know if shim code needed when using reactnativify, , couldn't find sources, ...
my second question: how use
react-native-crypto
in 'the proper reactnativify way'?
i've created github issues in reactnativify , react-native-crypto repo's:
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
Comments
Post a Comment