Projekt

Obecné

Profil

Stáhnout (1.58 KB) Statistiky
| Větev: | Revize:
1
var test = require('tape')
2
var crypto = require('diffie-hellman/browser')
3

    
4
test('diffie-hellman mod groups', function (t) {
5
  [
6
    'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'
7
  ].forEach(function (mod) {
8
    t.test(mod, function (t) {
9
      t.plan(3)
10
      var dh1 = crypto.getDiffieHellman(mod)
11
      var p1 = dh1.getPrime().toString('hex')
12
      dh1.generateKeys()
13
      var dh2 = crypto.getDiffieHellman(mod)
14
      var p2 = dh2.getPrime().toString('hex')
15
      dh2.generateKeys()
16
      t.equals(p1, p2, 'equal primes')
17
      var pubk1 = dh1.getPublicKey()
18
      var pubk2 = dh2.getPublicKey()
19
      t.notEquals(pubk1, pubk2, 'diff public keys')
20
      var pub1 = dh1.computeSecret(pubk2).toString('hex')
21
      var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex')
22
      t.equals(pub1, pub2, 'equal secrets')
23
    })
24
  })
25
})
26

    
27
test('diffie-hellman key lengths', function (t) {
28
  [
29
    64, 65, 192
30
  ].forEach(function (len) {
31
    t.test('' + len, function (t) {
32
      t.plan(3)
33
      var dh2 = crypto.createDiffieHellman(len)
34
      var prime2 = dh2.getPrime()
35
      var p2 = prime2.toString('hex')
36
      var dh1 = crypto.createDiffieHellman(prime2)
37
      var p1 = dh1.getPrime().toString('hex')
38
      dh1.generateKeys()
39
      dh2.generateKeys()
40
      t.equals(p1, p2, 'equal primes')
41
      var pubk1 = dh1.getPublicKey()
42
      var pubk2 = dh2.getPublicKey()
43
      t.notEquals(pubk1, pubk2, 'diff public keys')
44
      var pub1 = dh1.computeSecret(pubk2).toString('hex')
45
      var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex')
46
      t.equals(pub1, pub2, 'equal secrets')
47
    })
48
  })
49
})
(4-4/11)