From 3538ae2a3d630a2f0acc4d46b88c9d29ab7dd071 Mon Sep 17 00:00:00 2001 From: dryajov Date: Sun, 22 Jan 2017 19:33:05 -0800 Subject: [PATCH 1/2] fix support for WebWorkers --- src/crypto-browser.js | 15 ++++++++------- src/crypto-sha1-2-browser.js | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/crypto-browser.js b/src/crypto-browser.js index 9035e8ba..c6278740 100644 --- a/src/crypto-browser.js +++ b/src/crypto-browser.js @@ -1,3 +1,5 @@ +/* global self */ + 'use strict' const SHA3 = require('browserify-sha3') @@ -6,14 +8,13 @@ const nodeify = require('nodeify') const webCrypto = getWebCrypto() function getWebCrypto () { - if (typeof window !== 'undefined') { - if (window.crypto) { - return window.crypto.subtle || window.crypto.webkitSubtle - } + const globalCtx = typeof window !== 'undefined' ? window : self + if (globalCtx.crypto) { + return globalCtx.crypto.subtle || globalCtx.crypto.webkitSubtle + } - if (window.msCrypto) { - return window.msCrypto.subtle - } + if (globalCtx.msCrypto) { + return globalCtx.msCrypto.subtle } } diff --git a/src/crypto-sha1-2-browser.js b/src/crypto-sha1-2-browser.js index 652cca93..fc846b15 100644 --- a/src/crypto-sha1-2-browser.js +++ b/src/crypto-sha1-2-browser.js @@ -1,3 +1,5 @@ +/* global self */ + 'use strict' const nodeify = require('nodeify') @@ -5,14 +7,13 @@ const nodeify = require('nodeify') const webCrypto = getWebCrypto() function getWebCrypto () { - if (typeof window !== 'undefined') { - if (window.crypto) { - return window.crypto.subtle || window.crypto.webkitSubtle - } + const globalCtx = typeof window !== 'undefined' ? window : self + if (globalCtx.crypto) { + return globalCtx.crypto.subtle || globalCtx.crypto.webkitSubtle + } - if (window.msCrypto) { - return window.msCrypto.subtle - } + if (globalCtx.msCrypto) { + return globalCtx.msCrypto.subtle } } From 47cf94e77f029104cc5d90532626e2812693bdc9 Mon Sep 17 00:00:00 2001 From: dryajov Date: Thu, 26 Jan 2017 18:17:34 -0800 Subject: [PATCH 2/2] removing unused crypto-browser.js file --- src/crypto-browser.js | 69 ------------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 src/crypto-browser.js diff --git a/src/crypto-browser.js b/src/crypto-browser.js deleted file mode 100644 index c6278740..00000000 --- a/src/crypto-browser.js +++ /dev/null @@ -1,69 +0,0 @@ -/* global self */ - -'use strict' - -const SHA3 = require('browserify-sha3') -const nodeify = require('nodeify') - -const webCrypto = getWebCrypto() - -function getWebCrypto () { - const globalCtx = typeof window !== 'undefined' ? window : self - if (globalCtx.crypto) { - return globalCtx.crypto.subtle || globalCtx.crypto.webkitSubtle - } - - if (globalCtx.msCrypto) { - return globalCtx.msCrypto.subtle - } -} - -function webCryptoHash (type) { - if (!webCrypto) { - throw new Error('Please use a browser with webcrypto support') - } - - return (data, callback) => { - const res = webCrypto.digest({ name: type }, data) - - if (typeof res.then !== 'function') { // IE11 - res.onerror = () => { - callback(`Error hashing data using ${type}`) - } - res.oncomplete = (e) => { - callback(null, e.target.result) - } - return - } - - nodeify( - res.then((raw) => new Buffer(new Uint8Array(raw))), - callback - ) - } -} - -function sha1 (buf, callback) { - webCryptoHash('SHA-1')(buf, callback) -} - -function sha2256 (buf, callback) { - webCryptoHash('SHA-256')(buf, callback) -} - -function sha2512 (buf, callback) { - webCryptoHash('SHA-512')(buf, callback) -} - -function sha3 (buf, callback) { - const d = new SHA3.SHA3Hash() - const digest = new Buffer(d.update(buf).digest('hex'), 'hex') - callback(null, digest) -} - -module.exports = { - sha1: sha1, - sha2256: sha2256, - sha2512: sha2512, - sha3: sha3 -}