dh.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var test = require('tape')
  2. var cryptoB = require('../../')
  3. var crypto = require('crypto')
  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 = cryptoB.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(pubk1).toString('hex')
  22. t.equals(pub1, pub2, 'equal secrets')
  23. })
  24. })
  25. })
  26. test('diffie-hellman key lengths', function (t) {
  27. [
  28. 64, 65, 192
  29. ].forEach(function (len) {
  30. t.test('' + len, function (t) {
  31. t.plan(3)
  32. var dh2 = cryptoB.createDiffieHellman(len)
  33. var prime2 = dh2.getPrime()
  34. var p2 = prime2.toString('hex')
  35. var dh1 = crypto.createDiffieHellman(prime2)
  36. var p1 = dh1.getPrime().toString('hex')
  37. dh1.generateKeys()
  38. dh2.generateKeys()
  39. t.equals(p1, p2, 'equal primes')
  40. var pubk1 = dh1.getPublicKey()
  41. var pubk2 = dh2.getPublicKey()
  42. t.notEquals(pubk1, pubk2, 'diff public keys')
  43. var pub1 = dh1.computeSecret(pubk2).toString('hex')
  44. var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex')
  45. t.equals(pub1, pub2, 'equal secrets')
  46. })
  47. })
  48. })