Skip to content

Commit ac58988

Browse files
committed
ecdh
1 parent 347471d commit ac58988

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ exports.pbkdf2Sync = p.pbkdf2Sync
3838
require('browserify-aes/inject')(exports, module.exports);
3939
require('browserify-sign/inject')(module.exports, exports);
4040
require('diffie-hellman/inject')(exports, module.exports);
41+
require('create-ecdh/inject')(module.exports, exports);
4142

4243
// the least I can do is make error messages for the rest of the node.js/crypto api.
4344
each([
44-
'createCredentials',
45+
'createCredentials', 'publicEncrypt', 'privateDecrypt'
4546
], function (name) {
4647
exports[name] = function () {
4748
error('sorry,', name, 'is not implemented yet')

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
"dependencies": {
1919
"browserify-aes": "0.4.0",
2020
"browserify-sign": "2.4.0",
21+
"create-ecdh": "1.0.0",
22+
"diffie-hellman": "2.2.0",
2123
"pbkdf2-compat": "2.0.1",
2224
"ripemd160": "0.2.0",
23-
"diffie-hellman": "2.2.0",
2425
"sha.js": "~2.2.7"
2526
},
2627
"devDependencies": {

readme.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ Here is the subset that is currently implemented:
1919
* createCipher (aes)
2020
* createDecipher (aes)
2121
* createDiffieHellman
22+
* createECDH (secp256k1)
2223

2324
## todo
2425

2526
these features from node's `crypto` are still unimplemented.
2627

2728
* createCredentials
29+
* publicEncrypt/privateDecrypt
2830

2931
## contributions
3032

test/ecdh.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var mods = [
2+
'secp256k1'
3+
];
4+
var test = require('tape');
5+
var crypto = require('../');
6+
test('createECDH', function (t) {
7+
mods.forEach(function (mod) {
8+
t.test(mod + ' uncompressed', function (t){
9+
t.plan(2);
10+
var dh1 = crypto.createECDH(mod);
11+
dh1.generateKeys();
12+
var dh2 = crypto.createECDH(mod);
13+
dh2.generateKeys();
14+
var pubk1 = dh1.getPublicKey();
15+
var pubk2 = dh2.getPublicKey();
16+
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys');
17+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
18+
var pub2 = dh2.computeSecret(pubk1).toString('hex');
19+
t.equals(pub1, pub2, 'equal secrets');
20+
});
21+
t.test(mod + ' compressed', function (t){
22+
t.plan(2);
23+
var dh1 = crypto.createECDH(mod);
24+
dh1.generateKeys();
25+
var dh2 = crypto.createECDH(mod);
26+
dh2.generateKeys();
27+
var pubk1 = dh1.getPublicKey(null, 'compressed');
28+
var pubk2 = dh2.getPublicKey(null, 'compressed');
29+
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys');
30+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
31+
var pub2 = dh2.computeSecret(pubk1).toString('hex');
32+
t.equals(pub1, pub2, 'equal secrets');
33+
});
34+
t.test(mod + ' set stuff', function (t){
35+
t.plan(5);
36+
var dh1 = crypto.createECDH(mod);
37+
var dh2 = crypto.createECDH(mod);
38+
dh1.generateKeys();
39+
dh2.generateKeys();
40+
dh1.setPrivateKey(dh2.getPrivateKey());
41+
dh1.setPublicKey(dh2.getPublicKey());
42+
var priv1 = dh1.getPrivateKey('hex');
43+
var priv2 = dh2.getPrivateKey('hex');
44+
t.equals(priv1, priv2, 'same private key');
45+
var pubk1 = dh1.getPublicKey();
46+
var pubk2 = dh2.getPublicKey();
47+
t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed');
48+
t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed');
49+
t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid');
50+
var pub1 = dh1.computeSecret(pubk2).toString('hex');
51+
var pub2 = dh2.computeSecret(pubk1).toString('hex');
52+
t.equals(pub1, pub2, 'equal secrets');
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)