Skip to content

Commit

Permalink
imported isomorphic to getRandomValues for react native
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdul Haseeb committed Aug 5, 2021
1 parent 204384f commit c4b2a18
Show file tree
Hide file tree
Showing 3 changed files with 9,514 additions and 19 deletions.
52 changes: 33 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@
import { randomBytes } from 'react-native-randombytes'
exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = randomBytes

// implement window.getRandomValues(), for packages that rely on it
if (typeof window === 'object') {
if (!window.crypto) window.crypto = {}
if (!window.crypto.getRandomValues) {
window.crypto.getRandomValues = function getRandomValues (arr) {
let orig = arr
if (arr.byteLength != arr.length) {
// Get access to the underlying raw bytes
arr = new Uint8Array(arr.buffer)
}
const bytes = randomBytes(arr.length)
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes[i]
}

return orig
}
}
}
const crypto = require('isomorphic-webcrypto')

(async () => {
// Only needed for crypto.getRandomValues
// but only wait once, future calls are secure
await crypto.ensureSecure();
const array = new Uint8Array(1);
crypto.getRandomValues(array);
const safeValue = array[0];

return array;
})()


// // implement window.getRandomValues(), for packages that rely on it
// if (typeof window === 'object') {
// if (!window.crypto) window.crypto = {}
// if (!window.crypto.getRandomValues) {
// window.crypto.getRandomValues = function getRandomValues (arr) {
// let orig = arr
// if (arr.byteLength != arr.length) {
// // Get access to the underlying raw bytes
// arr = new Uint8Array(arr.buffer)
// }
// const bytes = randomBytes(arr.length)
// for (var i = 0; i < bytes.length; i++) {
// arr[i] = bytes[i]
// }

// return orig
// }
// }
// }

exports.createHash = exports.Hash = require('create-hash')
exports.createHmac = exports.Hmac = require('create-hmac')
Expand Down
Loading

0 comments on commit c4b2a18

Please sign in to comment.