From 8d8294dc3f9b15d827536f5f79fb512eaf4384a9 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 8 Jan 2019 18:37:03 +0000 Subject: [PATCH] fix: clean up, bundle size reduction BREAKING CHANGE: getRandomValues method exported from src/keys/rsa-browser.js and src/keys/rsa.js signature has changed from accepting an array to a number for random byte length --- .npmignore | 34 ---------------------------------- package.json | 20 +++++++++++++------- src/hmac/index-browser.js | 2 +- src/keys/ecdh-browser.js | 2 +- src/keys/index.js | 5 ++++- src/keys/rsa-browser.js | 8 +++----- src/keys/rsa-class.js | 7 ++++--- src/keys/rsa.js | 5 ++--- src/pbkdf2.js | 7 ++++--- src/random-bytes.js | 10 +++------- src/webcrypto.js | 13 +------------ 11 files changed, 36 insertions(+), 77 deletions(-) delete mode 100644 .npmignore diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 59335fda..00000000 --- a/.npmignore +++ /dev/null @@ -1,34 +0,0 @@ -**/node_modules/ -**/*.log -test/repo-tests* - -# Logs -logs -*.log - -coverage - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -build - -# Dependency directory -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git -node_modules - -test diff --git a/package.json b/package.json index c21a479d..c9cef3e2 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,10 @@ "./src/aes/ciphers.js": "./src/aes/ciphers-browser.js", "./src/keys/rsa.js": "./src/keys/rsa-browser.js" }, + "files": [ + "src", + "dist" + ], "scripts": { "lint": "aegir lint", "build": "aegir build", @@ -20,7 +24,8 @@ "release": "aegir release", "release-minor": "aegir release --type minor", "release-major": "aegir release --type major", - "coverage": "aegir coverage --ignore src/keys/keys.proto.js" + "coverage": "aegir coverage --ignore src/keys/keys.proto.js", + "size": "bundlesize -f dist/index.min.js -s 139kB" }, "keywords": [ "IPFS", @@ -34,27 +39,28 @@ "async": "^2.6.1", "browserify-aes": "^1.2.0", "bs58": "^4.0.1", + "iso-random-stream": "^1.1.0", "keypair": "^1.0.1", - "libp2p-crypto-secp256k1": "~0.2.2", + "libp2p-crypto-secp256k1": "~0.2.3", "multihashing-async": "~0.5.1", "node-forge": "~0.7.6", "pem-jwk": "^1.5.1", "protons": "^1.0.1", "rsa-pem-to-jwk": "^1.1.3", "tweetnacl": "^1.0.0", - "ursa-optional": "~0.9.9", - "webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master" + "ursa-optional": "~0.9.9" }, "devDependencies": { - "aegir": "^17.1.1", + "aegir": "^18.0.3", "benchmark": "^2.1.4", + "bundlesize": "~0.17.0", "chai": "^4.2.0", "chai-string": "^1.5.0", "dirty-chai": "^2.0.1" }, "engines": { - "node": ">=6.0.0", - "npm": ">=3.0.0" + "node": ">=10.0.0", + "npm": ">=6.0.0" }, "repository": { "type": "git", diff --git a/src/hmac/index-browser.js b/src/hmac/index-browser.js index 610e8cad..c3c40c33 100644 --- a/src/hmac/index-browser.js +++ b/src/hmac/index-browser.js @@ -2,7 +2,7 @@ const nodeify = require('../nodeify') -const crypto = require('../webcrypto.js')() +const crypto = require('../webcrypto') const lengths = require('./lengths') const hashTypes = { diff --git a/src/keys/ecdh-browser.js b/src/keys/ecdh-browser.js index 33713720..ab54759a 100644 --- a/src/keys/ecdh-browser.js +++ b/src/keys/ecdh-browser.js @@ -1,6 +1,6 @@ 'use strict' -const webcrypto = require('../webcrypto.js')() +const webcrypto = require('../webcrypto') const nodeify = require('../nodeify') const BN = require('asn1.js').bignum diff --git a/src/keys/index.js b/src/keys/index.js index b0f576e1..7d3e3c6c 100644 --- a/src/keys/index.js +++ b/src/keys/index.js @@ -2,7 +2,10 @@ const protobuf = require('protons') const keysPBM = protobuf(require('./keys.proto')) -const forge = require('node-forge') +require('node-forge/lib/asn1') +require('node-forge/lib/rsa') +require('node-forge/lib/pbe') +const forge = require('node-forge/lib/forge') exports = module.exports diff --git a/src/keys/rsa-browser.js b/src/keys/rsa-browser.js index e14ea96e..6cf12865 100644 --- a/src/keys/rsa-browser.js +++ b/src/keys/rsa-browser.js @@ -1,8 +1,8 @@ 'use strict' const nodeify = require('../nodeify') - -const webcrypto = require('../webcrypto.js')() +const webcrypto = require('../webcrypto') +const randomBytes = require('../random-bytes') exports.utils = require('./rsa-utils') @@ -49,9 +49,7 @@ exports.unmarshalPrivateKey = function (key, callback) { })), callback) } -exports.getRandomValues = function (arr) { - return Buffer.from(webcrypto.getRandomValues(arr)) -} +exports.getRandomValues = randomBytes exports.hashAndSign = function (key, msg, callback) { nodeify(webcrypto.subtle.importKey( diff --git a/src/keys/rsa-class.js b/src/keys/rsa-class.js index 46c245ea..ff67731e 100644 --- a/src/keys/rsa-class.js +++ b/src/keys/rsa-class.js @@ -7,7 +7,9 @@ const nextTick = require('async/nextTick') const crypto = require('./rsa') const pbm = protobuf(require('./keys.proto')) -const forge = require('node-forge') +require('node-forge/lib/sha512') +require('node-forge/lib/pbe') +const forge = require('node-forge/lib/forge') class RsaPublicKey { constructor (key) { @@ -53,7 +55,7 @@ class RsaPrivateKey { } genSecret () { - return crypto.getRandomValues(new Uint8Array(16)) + return crypto.getRandomValues(16) } sign (message, callback) { @@ -136,7 +138,6 @@ class RsaPrivateKey { const buffer = new forge.util.ByteBuffer(this.marshal()) const asn1 = forge.asn1.fromDer(buffer) const privateKey = forge.pki.privateKeyFromAsn1(asn1) - if (format === 'pkcs-8') { const options = { algorithm: 'aes256', diff --git a/src/keys/rsa.js b/src/keys/rsa.js index 0137135c..67ea7665 100644 --- a/src/keys/rsa.js +++ b/src/keys/rsa.js @@ -1,6 +1,7 @@ 'use strict' const crypto = require('crypto') +const randomBytes = require('../random-bytes') const nextTick = require('async/nextTick') let keypair @@ -63,9 +64,7 @@ exports.unmarshalPrivateKey = function (key, callback) { }) } -exports.getRandomValues = function (arr) { - return crypto.randomBytes(arr.length) -} +exports.getRandomValues = randomBytes exports.hashAndSign = function (key, msg, callback) { nextTick(() => { diff --git a/src/pbkdf2.js b/src/pbkdf2.js index 993595a5..63ebfbfd 100644 --- a/src/pbkdf2.js +++ b/src/pbkdf2.js @@ -1,6 +1,7 @@ 'use strict' -const forge = require('node-forge') +const forgePbkdf2 = require('node-forge/lib/pbkdf2') +const forgeUtil = require('node-forge/lib/util') /** * Maps an IPFS hash name to its node-forge equivalent. @@ -30,13 +31,13 @@ function pbkdf2 (password, salt, iterations, keySize, hash) { if (!hasher) { throw new Error(`Hash '${hash}' is unknown or not supported`) } - const dek = forge.pkcs5.pbkdf2( + const dek = forgePbkdf2( password, salt, iterations, keySize, hasher) - return forge.util.encode64(dek) + return forgeUtil.encode64(dek) } module.exports = pbkdf2 diff --git a/src/random-bytes.js b/src/random-bytes.js index 77ab3fcc..78c1288b 100644 --- a/src/random-bytes.js +++ b/src/random-bytes.js @@ -1,13 +1,9 @@ 'use strict' +const randomBytes = require('iso-random-stream/src/random') -const rsa = require('./keys/rsa') - -function randomBytes (number) { +module.exports = function (number) { if (!number || typeof number !== 'number') { throw new Error('first argument must be a Number bigger than 0') } - - return rsa.getRandomValues(new Uint8Array(number)) + return randomBytes(number) } - -module.exports = randomBytes diff --git a/src/webcrypto.js b/src/webcrypto.js index c736f024..0f9a557b 100644 --- a/src/webcrypto.js +++ b/src/webcrypto.js @@ -2,15 +2,4 @@ 'use strict' -module.exports = () => { - // This is only a shim for interfaces, not for functionality - if (typeof self !== 'undefined') { - require('webcrypto-shim')(self) - - if (self.crypto) { - return self.crypto - } - } - - throw new Error('Please use an environment with crypto support') -} +module.exports = self.crypto || self.msCrypto