From 92f94a800cd6a57d697744da8e638b62213de434 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 16 Aug 2017 15:21:30 +0200 Subject: [PATCH 1/2] quick hack --- src/hmac/index-browser.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/hmac/index-browser.js b/src/hmac/index-browser.js index 2ac5cd6c..4c30978f 100644 --- a/src/hmac/index-browser.js +++ b/src/hmac/index-browser.js @@ -1,6 +1,6 @@ 'use strict' -const nodeify = require('nodeify') +// const nodeify = require('nodeify') const Buffer = require('safe-buffer').Buffer const crypto = require('../webcrypto.js')() @@ -12,6 +12,14 @@ const hashTypes = { SHA512: 'SHA-512' } +function nodeify (promise, cb) { + promise.then((res) => { + cb(null, res) + }, (err) => { + cb(err) + }) +} + exports.create = function (hashType, secret, callback) { const hash = hashTypes[hashType] From 9168137881618af175ea439b5d8d562672738110 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 17 Aug 2017 01:24:17 +0200 Subject: [PATCH 2/2] cleanup hack --- package.json | 7 +++---- src/hmac/index-browser.js | 14 +++++--------- src/keys/ecdh-browser.js | 2 +- src/keys/rsa-browser.js | 2 +- src/nodeify.js | 11 +++++++++++ 5 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 src/nodeify.js diff --git a/package.json b/package.json index 4905e9b6..964f9cb5 100644 --- a/package.json +++ b/package.json @@ -35,14 +35,13 @@ "browserify-aes": "^1.0.6", "keypair": "^1.0.1", "libp2p-crypto-secp256k1": "~0.2.1", - "nodeify": "^1.0.1", + "multihashing-async": "~0.4.6", "pem-jwk": "^1.5.1", "protocol-buffers": "^3.2.1", "rsa-pem-to-jwk": "^1.1.3", "safe-buffer": "^5.1.1", "tweetnacl": "^1.0.0", - "webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master", - "multihashing-async": "~0.4.6" + "webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master" }, "devDependencies": { "aegir": "^11.0.2", @@ -79,4 +78,4 @@ "greenkeeper[bot] ", "nikuda " ] -} \ No newline at end of file +} diff --git a/src/hmac/index-browser.js b/src/hmac/index-browser.js index 4c30978f..90634c0e 100644 --- a/src/hmac/index-browser.js +++ b/src/hmac/index-browser.js @@ -1,6 +1,6 @@ 'use strict' -// const nodeify = require('nodeify') +const nodeify = require('../nodeify') const Buffer = require('safe-buffer').Buffer const crypto = require('../webcrypto.js')() @@ -12,12 +12,9 @@ const hashTypes = { SHA512: 'SHA-512' } -function nodeify (promise, cb) { - promise.then((res) => { - cb(null, res) - }, (err) => { - cb(err) - }) +const sign = (key, data, cb) => { + nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data) + .then((raw) => Buffer.from(raw)), cb) } exports.create = function (hashType, secret, callback) { @@ -35,8 +32,7 @@ exports.create = function (hashType, secret, callback) { ).then((key) => { return { digest (data, cb) { - nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data) - .then((raw) => Buffer.from(raw)), cb) + sign(key, data, cb) }, length: lengths[hashType] } diff --git a/src/keys/ecdh-browser.js b/src/keys/ecdh-browser.js index c63f4da3..959288dc 100644 --- a/src/keys/ecdh-browser.js +++ b/src/keys/ecdh-browser.js @@ -1,7 +1,7 @@ 'use strict' const webcrypto = require('../webcrypto.js')() -const nodeify = require('nodeify') +const nodeify = require('../nodeify') const BN = require('asn1.js').bignum const Buffer = require('safe-buffer').Buffer diff --git a/src/keys/rsa-browser.js b/src/keys/rsa-browser.js index 8df964a0..f5a1abae 100644 --- a/src/keys/rsa-browser.js +++ b/src/keys/rsa-browser.js @@ -1,6 +1,6 @@ 'use strict' -const nodeify = require('nodeify') +const nodeify = require('../nodeify') const Buffer = require('safe-buffer').Buffer const webcrypto = require('../webcrypto.js')() diff --git a/src/nodeify.js b/src/nodeify.js new file mode 100644 index 00000000..4a741b39 --- /dev/null +++ b/src/nodeify.js @@ -0,0 +1,11 @@ +'use strict' + +// Based on npmjs.com/nodeify but without additional `nextTick` calls +// to keep the overhead low +module.exports = function nodeify (promise, cb) { + return promise.then((res) => { + cb(null, res) + }, (err) => { + cb(err) + }) +}