dh.js 1.6 KB

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