Cómo parchear / calzar crypto.getRandomValues para React Native

Estoy portando algunos paquetes creados para NodeJS para React Native usandoReaccionarNativificar para reescribir las dependencias del objeto API Node en sus equivalentes de navegador.

Uno de ellos escrypto. Entransformer.js (o.babelrc) Yo tengo:

// 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',
    // ...
  },
  throwForNonStringLiteral: true,
}],

En ReactNativifyglobal.js existe este código (que excluí, porque no está destinado a la producción):

// 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());
    }
  },
};

.

Mi primera pregunta: Como esgetRandomValues parchado correctamente para la producción?

Hay una segunda opción, y es usarreact-native-crypto (un clon decrypto-browserify)

Idealmente debería poder hacer esto entransformer.js:

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

Peroreact-native-crypto usosrn-nodeify en lugar de ReactNativify, que genera unshim.js para ser importado enindex.android.js / index.ios.js con código similar a este:

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
    }
}

No sé si se necesita todo este código shim cuando se usa ReactNativify, y no pude encontrar buenas fuentes, así que ...

Mi segunda pregunta: Cómo utilizarreact-native-crypto en 'la forma correcta de ReactNativify'?

He creado problemas de github en ReactNativify y react-native-crypto repo's:

¿Cómo usar getRandomValues en producción?¿Usa ReactNativify en lugar de rn-nodeify?

Versiones

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

Respuestas a la pregunta(0)

Su respuesta a la pregunta