Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: clean up, bundle size reduction
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hugomrdias authored and daviddias committed Jan 8, 2019
1 parent df23d63 commit 8d8294d
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 77 deletions.
34 changes: 0 additions & 34 deletions .npmignore

This file was deleted.

20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/hmac/index-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const nodeify = require('../nodeify')

const crypto = require('../webcrypto.js')()
const crypto = require('../webcrypto')
const lengths = require('./lengths')

const hashTypes = {
Expand Down
2 changes: 1 addition & 1 deletion src/keys/ecdh-browser.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/keys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions src/keys/rsa-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -53,7 +55,7 @@ class RsaPrivateKey {
}

genSecret () {
return crypto.getRandomValues(new Uint8Array(16))
return crypto.getRandomValues(16)
}

sign (message, callback) {
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 2 additions & 3 deletions src/keys/rsa.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const crypto = require('crypto')
const randomBytes = require('../random-bytes')
const nextTick = require('async/nextTick')

let keypair
Expand Down Expand Up @@ -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(() => {
Expand Down
7 changes: 4 additions & 3 deletions src/pbkdf2.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
10 changes: 3 additions & 7 deletions src/random-bytes.js
Original file line number Diff line number Diff line change
@@ -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
13 changes: 1 addition & 12 deletions src/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8d8294d

Please sign in to comment.