Skip to content

Commit

Permalink
export crypto.subtle from isomorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdul Haseeb committed Aug 6, 2021
1 parent d85a544 commit a9c2786
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,27 @@
import { randomBytes } from 'react-native-randombytes'
import crypto from 'isomorphic-webcrypto'
exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = randomBytes


(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.subtle = crypto.subtle

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

0 comments on commit a9c2786

Please sign in to comment.