Skip to content

Commit

Permalink
fix crypto shim
Browse files Browse the repository at this point in the history
  • Loading branch information
mvayngrib committed Jan 22, 2017
1 parent 48f57b1 commit e6e4e08
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if (typeof localStorage !== 'undefined') {
}

if (require('./package.json').dependencies['react-native-crypto']) {
// important that this comes before require('crypto')
const algos = require('browserify-sign/algos')
if (!algos.sha256) {
algos.sha256 = {
Expand All @@ -31,17 +32,25 @@ if (require('./package.json').dependencies['react-native-crypto']) {
}
}

const randomBytes = require('react-native-randombytes').randomBytes

let crypto
if (typeof window === 'object') {
const wCrypto = window.crypto = window.crypto || {}
if (!wCrypto.getRandomValues) {
wCrypto.getRandomValues = function getRandomValues (arr) {
const bytes = randomBytes(arr.length)
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes[i]
}
}
crypto = window.crypto = window.crypto || {}
} else {
crypto = require('crypto')
}

if (!crypto.getRandomValues) {
crypto.getRandomValues = getRandomValues
}

let randomBytes

function getRandomValues (arr) {
if (!randomBytes) randomBytes = require('react-native-randombytes').randomBytes

const bytes = randomBytes(arr.length)
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes[i]
}
}
}

0 comments on commit e6e4e08

Please sign in to comment.