UNPKG

1.63 kBJavaScriptView Raw
1var test = require('tape')
2var cryptoB = require('../../')
3var crypto = require('crypto')
4
5test('diffie-hellman mod groups', function (t) {
6 [
7 'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'
8 ].forEach(function (mod) {
9 t.test(mod, function (t) {
10 t.plan(3)
11 var dh1 = cryptoB.getDiffieHellman(mod)
12 var p1 = dh1.getPrime().toString('hex')
13 dh1.generateKeys()
14
15 var dh2 = crypto.getDiffieHellman(mod)
16 var p2 = dh2.getPrime().toString('hex')
17 dh2.generateKeys()
18 t.equals(p1, p2, 'equal primes')
19 var pubk1 = dh1.getPublicKey()
20 var pubk2 = dh2.getPublicKey()
21 t.notEquals(pubk1, pubk2, '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})
28
29test('diffie-hellman key lengths', function (t) {
30 [
31 64, 65, 192
32 ].forEach(function (len) {
33 t.test('' + len, function (t) {
34 t.plan(3)
35 var dh2 = cryptoB.createDiffieHellman(len)
36 var prime2 = dh2.getPrime()
37 var p2 = prime2.toString('hex')
38 var dh1 = crypto.createDiffieHellman(prime2)
39 var p1 = dh1.getPrime().toString('hex')
40 dh1.generateKeys()
41 dh2.generateKeys()
42 t.equals(p1, p2, 'equal primes')
43 var pubk1 = dh1.getPublicKey()
44 var pubk2 = dh2.getPublicKey()
45 t.notEquals(pubk1, pubk2, 'diff public keys')
46 var pub1 = dh1.computeSecret(pubk2).toString('hex')
47 var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex')
48 t.equals(pub1, pub2, 'equal secrets')
49 })
50 })
51})