You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

4 years ago
  1. var generatePrime = require('./lib/generatePrime')
  2. var primes = require('./lib/primes.json')
  3. var DH = require('./lib/dh')
  4. function getDiffieHellman (mod) {
  5. var prime = new Buffer(primes[mod].prime, 'hex')
  6. var gen = new Buffer(primes[mod].gen, 'hex')
  7. return new DH(prime, gen)
  8. }
  9. var ENCODINGS = {
  10. 'binary': true, 'hex': true, 'base64': true
  11. }
  12. function createDiffieHellman (prime, enc, generator, genc) {
  13. if (Buffer.isBuffer(enc) || ENCODINGS[enc] === undefined) {
  14. return createDiffieHellman(prime, 'binary', enc, generator)
  15. }
  16. enc = enc || 'binary'
  17. genc = genc || 'binary'
  18. generator = generator || new Buffer([2])
  19. if (!Buffer.isBuffer(generator)) {
  20. generator = new Buffer(generator, genc)
  21. }
  22. if (typeof prime === 'number') {
  23. return new DH(generatePrime(prime, generator), generator, true)
  24. }
  25. if (!Buffer.isBuffer(prime)) {
  26. prime = new Buffer(prime, enc)
  27. }
  28. return new DH(prime, generator, true)
  29. }
  30. exports.DiffieHellmanGroup = exports.createDiffieHellmanGroup = exports.getDiffieHellman = getDiffieHellman
  31. exports.createDiffieHellman = exports.DiffieHellman = createDiffieHellman