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

fix: add buffer, cleanup, reduce size #170

Merged
merged 5 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
bundlesize: { maxSize: '155kB' }
bundlesize: { maxSize: '124kB' }
}
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,20 @@
],
"license": "MIT",
"dependencies": {
"asmcrypto.js": "^2.3.2",
"bn.js": "^5.0.0",
"browserify-aes": "^1.2.0",
"bs58": "^4.0.1",
"buffer": "^5.5.0",
"err-code": "^2.0.0",
"iso-random-stream": "^1.1.0",
"keypair": "^1.0.1",
"libp2p-crypto-secp256k1": "~0.4.0",
"multihashing-async": "~0.8.0",
"libp2p-crypto-secp256k1": "libp2p/js-libp2p-crypto-secp256k1#fix/add-buffer",
hugomrdias marked this conversation as resolved.
Show resolved Hide resolved
"multibase": "^0.6.0",
"multihashing-async": "^0.8.1",
"node-forge": "~0.9.1",
"pem-jwk": "^2.0.0",
"protons": "^1.0.1",
"rsa-pem-to-jwk": "^1.1.3",
"tweetnacl": "^1.0.1",
"ursa-optional": "~0.10.1"
},
"devDependencies": {
"@types/bn.js": "^4.11.6",
"@types/chai": "^4.2.7",
"@types/chai": "^4.2.11",
"@types/chai-string": "^1.4.2",
"@types/dirty-chai": "^2.0.2",
"@types/mocha": "^7.0.1",
Expand Down
27 changes: 23 additions & 4 deletions src/aes/ciphers-browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
'use strict'

const crypto = require('browserify-aes')
const { Buffer } = require('buffer')
require('node-forge/lib/aes')
const forge = require('node-forge/lib/forge')

module.exports = {
createCipheriv: crypto.createCipheriv,
createDecipheriv: crypto.createDecipheriv
createCipheriv: (mode, key, iv) => {
const cipher2 = forge.cipher.createCipher('AES-CTR', key.toString('binary'))
cipher2.start({ iv: iv.toString('binary') })
return {
update: (data) => {
cipher2.update(forge.util.createBuffer(data.toString('binary')))
return Buffer.from(cipher2.output.getBytes(), 'binary')
}
}
},
createDecipheriv: (mode, key, iv) => {
const cipher2 = forge.cipher.createDecipher('AES-CTR', key.toString('binary'))
cipher2.start({ iv: iv.toString('binary') })
return {
update: (data) => {
cipher2.update(forge.util.createBuffer(data.toString('binary')))
return Buffer.from(cipher2.output.getBytes(), 'binary')
}
}
}
}
34 changes: 0 additions & 34 deletions src/aes/index-browser.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/hmac/index-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

const { Buffer } = require('buffer')
const webcrypto = require('../webcrypto')
const lengths = require('./lengths')

Expand Down
19 changes: 10 additions & 9 deletions src/keys/ed25519-class.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

const multihashing = require('multihashing-async')
const { Buffer } = require('buffer')
const sha = require('multihashing-async/src/sha')
const protobuf = require('protons')
const bs58 = require('bs58')
const multibase = require('multibase')
const errcode = require('err-code')

const crypto = require('./ed25519')
Expand Down Expand Up @@ -33,7 +34,7 @@ class Ed25519PublicKey {
}

async hash () { // eslint-disable-line require-await
return multihashing(this.bytes, 'sha2-256')
return sha.multihashing(this.bytes, 'sha2-256')
}
}

Expand Down Expand Up @@ -69,7 +70,7 @@ class Ed25519PrivateKey {
}

async hash () { // eslint-disable-line require-await
return multihashing(this.bytes, 'sha2-256')
return sha.multihashing(this.bytes, 'sha2-256')
}

/**
Expand All @@ -83,7 +84,7 @@ class Ed25519PrivateKey {
*/
async id () {
const hash = await this.public.hash()
return bs58.encode(hash)
return multibase.encode('base58btc', hash).toString().slice(1)
}
}

Expand All @@ -100,13 +101,13 @@ function unmarshalEd25519PublicKey (bytes) {
}

async function generateKeyPair () {
const { secretKey, publicKey } = await crypto.generateKey()
return new Ed25519PrivateKey(secretKey, publicKey)
const { privateKey, publicKey } = await crypto.generateKey()
return new Ed25519PrivateKey(privateKey, publicKey)
}

async function generateKeyPairFromSeed (seed) {
const { secretKey, publicKey } = await crypto.generateKeyFromSeed(seed)
return new Ed25519PrivateKey(secretKey, publicKey)
const { privateKey, publicKey } = await crypto.generateKeyFromSeed(seed)
return new Ed25519PrivateKey(privateKey, publicKey)
}

function ensureKey (key, length) {
Expand Down
17 changes: 9 additions & 8 deletions src/keys/ed25519.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
'use strict'

const nacl = require('tweetnacl')

exports.publicKeyLength = nacl.sign.publicKeyLength
exports.privateKeyLength = nacl.sign.secretKeyLength
require('node-forge/lib/ed25519')
const forge = require('node-forge/lib/forge')
exports.publicKeyLength = forge.pki.ed25519.constants.PUBLIC_KEY_BYTE_LENGTH
exports.privateKeyLength = forge.pki.ed25519.constants.PRIVATE_KEY_BYTE_LENGTH

exports.generateKey = async function () { // eslint-disable-line require-await
return nacl.sign.keyPair()
return forge.pki.ed25519.generateKeyPair()
}

// seed should be a 32 byte uint8array
exports.generateKeyFromSeed = async function (seed) { // eslint-disable-line require-await
return nacl.sign.keyPair.fromSeed(seed)
return forge.pki.ed25519.generateKeyPair({ seed })
}

exports.hashAndSign = async function (key, msg) { // eslint-disable-line require-await
return Buffer.from(nacl.sign.detached(msg, key))
return forge.pki.ed25519.sign({ message: msg, privateKey: key })
// return Buffer.from(nacl.sign.detached(msg, key))
}

exports.hashAndVerify = async function (key, sig, msg) { // eslint-disable-line require-await
return nacl.sign.detached.verify(msg, sig, key)
return forge.pki.ed25519.verify({ signature: sig, message: msg, publicKey: key })
}
2 changes: 1 addition & 1 deletion src/keys/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { Buffer } = require('buffer')
const protobuf = require('protons')
const keysPBM = protobuf(require('./keys.proto'))
require('node-forge/lib/asn1')
require('node-forge/lib/rsa')
require('node-forge/lib/pbe')
const forge = require('node-forge/lib/forge')
const errcode = require('err-code')
Expand Down
2 changes: 1 addition & 1 deletion src/keys/key-stretcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

const { Buffer } = require('buffer')
const errcode = require('err-code')
const hmac = require('../hmac')

Expand Down
1 change: 1 addition & 0 deletions src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Buffer } = require('buffer')
const webcrypto = require('../webcrypto')
const randomBytes = require('../random-bytes')

Expand Down
12 changes: 6 additions & 6 deletions src/keys/rsa-class.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

const multihashing = require('multihashing-async')
const sha = require('multihashing-async/src/sha')
const protobuf = require('protons')
const bs58 = require('bs58')
const multibase = require('multibase')
const errcode = require('err-code')

const crypto = require('./rsa')
const pbm = protobuf(require('./keys.proto'))
require('node-forge/lib/sha512')
require('node-forge/lib/pbe')
require('node-forge/lib/ed25519')
const forge = require('node-forge/lib/forge')

class RsaPublicKey {
Expand Down Expand Up @@ -40,7 +40,7 @@ class RsaPublicKey {
}

async hash () { // eslint-disable-line require-await
return multihashing(this.bytes, 'sha2-256')
return sha.multihashing(this.bytes, 'sha2-256')
}
}

Expand Down Expand Up @@ -88,7 +88,7 @@ class RsaPrivateKey {
}

async hash () { // eslint-disable-line require-await
return multihashing(this.bytes, 'sha2-256')
return sha.multihashing(this.bytes, 'sha2-256')
}

/**
Expand All @@ -102,7 +102,7 @@ class RsaPrivateKey {
*/
async id () {
const hash = await this.public.hash()
return bs58.encode(hash)
return multibase.encode('base58btc', hash).toString().slice(1)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/aes/aes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable valid-jsdoc */
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/go-elliptic-key.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const { Buffer } = require('buffer')

module.exports = {
curve: 'P-256',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/go-key-ed25519.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const { Buffer } = require('buffer')

module.exports = {
// These were generated in a gore (https://github.com/motemen/gore) repl session:
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/go-key-rsa.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/go-stretch-key.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

const { Buffer } = require('buffer')
module.exports = [{
cipher: 'AES-256',
hash: 'SHA256',
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/secp256k1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { Buffer } = require('buffer')
module.exports = {
// protobuf marshaled key pair generated with libp2p-crypto-secp256k1
// and marshaled with libp2p-crypto.marshalPublicKey / marshalPrivateKey
Expand Down
1 change: 1 addition & 0 deletions test/helpers/test-garbage-error-handling.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const util = require('util')
const garbage = [Buffer.from('00010203040506070809', 'hex'), {}, null, false, undefined, true, 1, 0, Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', '']

Expand Down
2 changes: 1 addition & 1 deletion test/hmac/hmac.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
Expand Down
1 change: 1 addition & 0 deletions test/keys/ed25519.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
Expand Down
1 change: 1 addition & 0 deletions test/keys/rsa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
Expand Down
1 change: 1 addition & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict'

const chai = require('chai')
const { Buffer } = require('buffer')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
Expand Down