Skip to content

Commit

Permalink
Private key import (#8)
Browse files Browse the repository at this point in the history
* easier private key import

* update travis

* use containers

* fix white space

* standard

* myfixes

* fix test to make sure we generate the same stuff as node

* get rid of end of life node versions
  • Loading branch information
calvinmetcalf committed May 7, 2018
1 parent 66dc7a2 commit a2c61b5
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 174 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
@@ -1,7 +1,7 @@
language: node_js

sudo: false
node_js:
- "0.10"
- "0.11"
- "0.12"
- "iojs"
- 6
- 8
- 9
- 10
206 changes: 104 additions & 102 deletions browser.js
@@ -1,122 +1,124 @@
var elliptic = require('elliptic');
var BN = require('bn.js');
var elliptic = require('elliptic')
var BN = require('bn.js')

module.exports = function createECDH(curve) {
return new ECDH(curve);
};
module.exports = function createECDH (curve) {
return new ECDH(curve)
}

var aliases = {
secp256k1: {
name: 'secp256k1',
byteLength: 32
},
secp224r1: {
name: 'p224',
byteLength: 28
},
prime256v1: {
name: 'p256',
byteLength: 32
},
prime192v1: {
name: 'p192',
byteLength: 24
},
ed25519: {
name: 'ed25519',
byteLength: 32
},
secp384r1: {
name: 'p384',
byteLength: 48
},
secp521r1: {
name: 'p521',
byteLength: 66
}
};
secp256k1: {
name: 'secp256k1',
byteLength: 32
},
secp224r1: {
name: 'p224',
byteLength: 28
},
prime256v1: {
name: 'p256',
byteLength: 32
},
prime192v1: {
name: 'p192',
byteLength: 24
},
ed25519: {
name: 'ed25519',
byteLength: 32
},
secp384r1: {
name: 'p384',
byteLength: 48
},
secp521r1: {
name: 'p521',
byteLength: 66
}
}

aliases.p224 = aliases.secp224r1;
aliases.p256 = aliases.secp256r1 = aliases.prime256v1;
aliases.p192 = aliases.secp192r1 = aliases.prime192v1;
aliases.p384 = aliases.secp384r1;
aliases.p521 = aliases.secp521r1;
aliases.p224 = aliases.secp224r1
aliases.p256 = aliases.secp256r1 = aliases.prime256v1
aliases.p192 = aliases.secp192r1 = aliases.prime192v1
aliases.p384 = aliases.secp384r1
aliases.p521 = aliases.secp521r1

function ECDH(curve) {
this.curveType = aliases[curve];
if (!this.curveType ) {
this.curveType = {
name: curve
};
}
this.curve = new elliptic.ec(this.curveType.name);
this.keys = void 0;
function ECDH (curve) {
this.curveType = aliases[curve]
if (!this.curveType) {
this.curveType = {
name: curve
}
}
this.curve = new elliptic.ec(this.curveType.name) // eslint-disable-line new-cap
this.keys = void 0
}

ECDH.prototype.generateKeys = function (enc, format) {
this.keys = this.curve.genKeyPair();
return this.getPublicKey(enc, format);
};
this.keys = this.curve.genKeyPair()
return this.getPublicKey(enc, format)
}

ECDH.prototype.computeSecret = function (other, inenc, enc) {
inenc = inenc || 'utf8';
if (!Buffer.isBuffer(other)) {
other = new Buffer(other, inenc);
}
var otherPub = this.curve.keyFromPublic(other).getPublic();
var out = otherPub.mul(this.keys.getPrivate()).getX();
return formatReturnValue(out, enc, this.curveType.byteLength);
};
inenc = inenc || 'utf8'
if (!Buffer.isBuffer(other)) {
other = new Buffer(other, inenc)
}
var otherPub = this.curve.keyFromPublic(other).getPublic()
var out = otherPub.mul(this.keys.getPrivate()).getX()
return formatReturnValue(out, enc, this.curveType.byteLength)
}

ECDH.prototype.getPublicKey = function (enc, format) {
var key = this.keys.getPublic(format === 'compressed', true);
if (format === 'hybrid') {
if (key[key.length - 1] % 2) {
key[0] = 7;
} else {
key [0] = 6;
}
}
return formatReturnValue(key, enc);
};
var key = this.keys.getPublic(format === 'compressed', true)
if (format === 'hybrid') {
if (key[key.length - 1] % 2) {
key[0] = 7
} else {
key[0] = 6
}
}
return formatReturnValue(key, enc)
}

ECDH.prototype.getPrivateKey = function (enc) {
return formatReturnValue(this.keys.getPrivate(), enc);
};
return formatReturnValue(this.keys.getPrivate(), enc)
}

ECDH.prototype.setPublicKey = function (pub, enc) {
enc = enc || 'utf8';
if (!Buffer.isBuffer(pub)) {
pub = new Buffer(pub, enc);
}
this.keys._importPublic(pub);
return this;
};
enc = enc || 'utf8'
if (!Buffer.isBuffer(pub)) {
pub = new Buffer(pub, enc)
}
this.keys._importPublic(pub)
return this
}

ECDH.prototype.setPrivateKey = function (priv, enc) {
enc = enc || 'utf8';
if (!Buffer.isBuffer(priv)) {
priv = new Buffer(priv, enc);
}
var _priv = new BN(priv);
_priv = _priv.toString(16);
this.keys._importPrivate(_priv);
return this;
};
enc = enc || 'utf8'
if (!Buffer.isBuffer(priv)) {
priv = new Buffer(priv, enc)
}

var _priv = new BN(priv)
_priv = _priv.toString(16)
this.keys = this.curve.genKeyPair()
this.keys._importPrivate(_priv)
return this
}

function formatReturnValue(bn, enc, len) {
if (!Array.isArray(bn)) {
bn = bn.toArray();
}
var buf = new Buffer(bn);
if (len && buf.length < len) {
var zeros = new Buffer(len - buf.length);
zeros.fill(0);
buf = Buffer.concat([zeros, buf]);
}
if (!enc) {
return buf;
} else {
return buf.toString(enc);
}
function formatReturnValue (bn, enc, len) {
if (!Array.isArray(bn)) {
bn = bn.toArray()
}
var buf = new Buffer(bn)
if (len && buf.length < len) {
var zeros = new Buffer(len - buf.length)
zeros.fill(0)
buf = Buffer.concat([zeros, buf])
}
if (!enc) {
return buf
} else {
return buf.toString(enc)
}
}
4 changes: 2 additions & 2 deletions index.js
@@ -1,3 +1,3 @@
var createECDH = require('crypto').createECDH;
var createECDH = require('crypto').createECDH

module.exports = createECDH || require('./browser');
module.exports = createECDH || require('./browser')
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"browser": "browser.js",
"scripts": {
"test": "node test.js | tspec"
"test": "standard && node test.js | tspec"
},
"repository": {
"type": "git",
Expand All @@ -25,7 +25,8 @@
"homepage": "https://github.com/crypto-browserify/createECDH",
"dependencies": {
"bn.js": "^4.1.0",
"elliptic": "^6.0.0"
"elliptic": "^6.0.0",
"standard": "^5.4.1"

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus May 7, 2018

@calvinmetcalf This should be in devDependencies

This comment has been minimized.

Copy link
@calvinmetcalf

calvinmetcalf May 7, 2018

Author Contributor

woops ok will fix

This comment has been minimized.

Copy link
@calvinmetcalf

calvinmetcalf May 7, 2018

Author Contributor

done

},
"devDependencies": {
"tap-spec": "^1.0.1",
Expand Down

0 comments on commit a2c61b5

Please sign in to comment.