Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Remove mitt dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlened committed Sep 9, 2019
1 parent 8408fc3 commit f048cc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
14 changes: 9 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"dependencies": {
"b64-lite": "^1.3.1",
"b64u-lite": "^1.0.1",
"mitt": "^1.1.3",
"msrcrypto": "^1.5.4",
"node-webcrypto-ossl": "^1.0.48",
"str2buf": "^1.3.0",
Expand Down
55 changes: 19 additions & 36 deletions src/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ if (require.getModules) {

if (!generateSecureRandom) {
console.log(`
isomorphic-webcrypto cannot ensure the security of some operations.
Please eject and run:
npm install react-native-securerandom --save
react-native link
Or install and configure expo-random: https://www.npmjs.com/package/expo-random
isomorphic-webcrypto cannot ensure the security of some operations!
Install and configure expo-random or react-native-securerandom
`);
generateSecureRandom = function(length) {
const uint8Array = new Uint8Array(length);
Expand All @@ -29,39 +24,28 @@ if (!generateSecureRandom) {
}
}

const EventEmitter = require('mitt');
const b64u = require('b64u-lite');
const str2buf = require('str2buf');
const b64u = require('b64u-lite');
const b64 = require('b64-lite');
global.atob = typeof atob === 'undefined' ? b64.atob : atob;
global.btoa = typeof btoa === 'undefined' ? b64.btoa : btoa;
global.msrCryptoPermanentForceSync = true;
const crypto = require('msrcrypto');

const secureWatch = new EventEmitter()

let secured = !crypto.initPrng
let secureRandomError
if (!secured) {
generateSecureRandom(48)
.then(byteArray => {
crypto.initPrng(Array.from(byteArray))
secured = true
secureWatch.emit('secure')
})
.catch(err => {
secureRandomError = err
secureWatch.emit('secureRandomError')
})
}

crypto.ensureSecure = () => new Promise((resolve, reject) => {
if (secured) return resolve();
if (secureRandomError) return reject(secureRandomError)
secureWatch.on('secure', () => resolve())
secureWatch.on('secureRandomError', () => reject(secureRandomError))
let isSecured = false;
const secured = new Promise((resolve, reject) => {
if (!crypto.initPrng) return resolve(false);
return generateSecureRandom(48)
.then(byteArray => {
crypto.initPrng(Array.from(byteArray))
isSecured = true;
resolve(true);
})
.catch(err => reject(err));
});

crypto.ensureSecure = () => secured;

function standardizeAlgoName(algo) {
const upper = algo.toUpperCase();
return upper === 'RSASSA-PKCS1-V1_5' ? 'RSASSA-PKCS1-v1_5' : upper;
Expand All @@ -78,13 +62,12 @@ function ensureUint8Array(buffer) {

const originalGetRandomValues = crypto.getRandomValues;
crypto.getRandomValues = function getRandomValues() {
if (!secured) {
if (!isSecured) {
throw new Error(`
You must wait until the library is secure to call this method:
crypto.ensureSecure(err => {
if (err) throw err;
const safeValues = crypto.getRandomValues();
});
await crypto.ensureSecure();
const safeValues = crypto.getRandomValues();
`);
}
return originalGetRandomValues.apply(crypto, arguments);
Expand Down

0 comments on commit f048cc5

Please sign in to comment.