1
|
var mods = [
|
2
|
'secp256k1',
|
3
|
'secp224r1',
|
4
|
'prime256v1',
|
5
|
'prime192v1'
|
6
|
]
|
7
|
var test = require('tape')
|
8
|
var _crypto = require('../')
|
9
|
var createECDH1 = _crypto.createECDH
|
10
|
var createECDH2 = require('create-ecdh/browser')
|
11
|
|
12
|
mods.forEach(function (mod) {
|
13
|
test('createECDH: ' + mod + ' uncompressed', function (t) {
|
14
|
t.plan(2)
|
15
|
var dh1 = createECDH1(mod)
|
16
|
dh1.generateKeys()
|
17
|
var dh2 = createECDH2(mod)
|
18
|
dh2.generateKeys()
|
19
|
var pubk1 = dh1.getPublicKey()
|
20
|
var pubk2 = dh2.getPublicKey()
|
21
|
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
|
22
|
var pub1 = dh1.computeSecret(pubk2).toString('hex')
|
23
|
var pub2 = dh2.computeSecret(pubk1).toString('hex')
|
24
|
t.equals(pub1, pub2, 'equal secrets')
|
25
|
})
|
26
|
|
27
|
test('createECDH: ' + mod + ' compressed', function (t) {
|
28
|
t.plan(2)
|
29
|
var dh1 = createECDH1(mod)
|
30
|
dh1.generateKeys()
|
31
|
var dh2 = createECDH2(mod)
|
32
|
dh2.generateKeys()
|
33
|
var pubk1 = dh1.getPublicKey(null, 'compressed')
|
34
|
var pubk2 = dh2.getPublicKey(null, 'compressed')
|
35
|
t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
|
36
|
var pub1 = dh1.computeSecret(pubk2).toString('hex')
|
37
|
var pub2 = dh2.computeSecret(pubk1).toString('hex')
|
38
|
t.equals(pub1, pub2, 'equal secrets')
|
39
|
})
|
40
|
|
41
|
test('createECDH: ' + mod + ' set stuff', function (t) {
|
42
|
t.plan(5)
|
43
|
var dh1 = createECDH1(mod)
|
44
|
var dh2 = createECDH2(mod)
|
45
|
dh1.generateKeys()
|
46
|
dh2.generateKeys()
|
47
|
dh1.setPrivateKey(dh2.getPrivateKey())
|
48
|
dh1.setPublicKey(dh2.getPublicKey())
|
49
|
var priv1 = dh1.getPrivateKey('hex')
|
50
|
var priv2 = dh2.getPrivateKey('hex')
|
51
|
t.equals(priv1, priv2, 'same private key')
|
52
|
var pubk1 = dh1.getPublicKey()
|
53
|
var pubk2 = dh2.getPublicKey()
|
54
|
t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed')
|
55
|
t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed')
|
56
|
t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid')
|
57
|
var pub1 = dh1.computeSecret(pubk2).toString('hex')
|
58
|
var pub2 = dh2.computeSecret(pubk1).toString('hex')
|
59
|
t.equals(pub1, pub2, 'equal secrets')
|
60
|
})
|
61
|
})
|