From 205e0ed1f710199e4afbc3480da58e042d899b11 Mon Sep 17 00:00:00 2001 From: Rob Wierzbowski Date: Wed, 25 Apr 2018 13:55:51 -0400 Subject: [PATCH] fix: Get correct version of IE11 crypto (#274) Users on Windows 7 and IE11 sometimes trigger a bind error because the crypto object exists but getRandomValues is undefined. Using https://github.com/openpgpjs/openpgpjs/pull/207 as a reference, I've added a more robust check for the necessary crypto properties in IE 11. --- lib/rng-browser.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/rng-browser.js b/lib/rng-browser.js index 14d21170..6361fb81 100644 --- a/lib/rng-browser.js +++ b/lib/rng-browser.js @@ -3,9 +3,11 @@ // and inconsistent support for the `crypto` API. We do the best we can via // feature-detection -// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. -var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues.bind(crypto)) || - (typeof(msCrypto) != 'undefined' && msCrypto.getRandomValues.bind(msCrypto)); +// getRandomValues needs to be invoked in a context where "this" is a Crypto +// implementation. Also, find the complete implementation of crypto on IE11. +var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto)) || + (typeof(msCrypto) != 'undefined' && typeof window.msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto)); + if (getRandomValues) { // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef