How to use the getDiffieHellman function from crypto

Find comprehensive JavaScript crypto.getDiffieHellman code examples handpicked from public code repositorys.

23
24
25
26
27
28
29
30
31
32
33
34
35


export async function createAndComputeDHSecret(){


    try{
        const prime = crypto.getDiffieHellman('modp15').getPrime()
        const gen = crypto.getDiffieHellman('modp15').getGenerator()


        const dh = crypto.createDiffieHellman(prime, gen);
        dh.generateKeys();

fork icon1
star icon0
watch icon0

+ 7 other calls in file

113
114
115
116
117
118
119
120
121
122
123
124
  'c4ce8628987d335466f4b16e1a04df21682d266eb3edf50b21802be3af58443c49da40529f' +
  '8f335a25134c1457787f60e7f0c481e32bb5c690354f68b4252936e2f4b61f9e63c76e3185' +
  '462d7e14f62c980a26f9da3837b2ff1b58e0aaa5d7464a7f8dcbc3a81d402dc6f28a42f4ec' +
  '55c6df68351ed9', 'hex');


const group = crypto.getDiffieHellman('modp5');
const dh = crypto.createDiffieHellman(group.getPrime(), group.getGenerator());
dh.setPrivateKey(privateKey);


// Test simple Diffie-Hellman, no curves involved.
fork icon0
star icon0
watch icon0

+ 7 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
var crypto = require('crypto')


// getDiffieHellman
exports.DiffieHellmanGroup = crypto.DiffieHellmanGroup
exports.createDiffieHellmanGroup = crypto.createDiffieHellmanGroup
exports.getDiffieHellman = crypto.getDiffieHellman


// createDiffieHellman
exports.createDiffieHellman = crypto.createDiffieHellman
exports.DiffieHellman = crypto.DiffieHellman
fork icon0
star icon0
watch icon0

+ 3 other calls in file

440
441
442
443
444
445
446
447
448
449
450
             'Received undefined'
  });


assert.throws(
  function() {
    crypto.getDiffieHellman('unknown-group');
  },
  {
    name: 'Error',
    code: 'ERR_CRYPTO_UNKNOWN_DH_GROUP',
fork icon0
star icon0
watch icon0

+ 20 other calls in file

41
42
43
44
45
46
47
48
49
50
// We can generate appropriate values with this code:
//
// crypto = require('crypto');
//
// for (;;) {
//   var a = crypto.getDiffieHellman('modp14'),
//   var b = crypto.getDiffieHellman('modp14');
//
//   a.generateKeys();
//   b.generateKeys();
fork icon0
star icon0
watch icon0

+ 11 other calls in file

42
43
44
45
46
47
48
49
50
51
52
  modp16: 'daea5277a7ad0116e734a8e0d2f297ef759d1161',
  modp17: '3b62aaf0142c2720f0bf26a9589b0432c00eadc1',
};


for (const name in hashes) {
  const group = crypto.getDiffieHellman(name);
  const private_key = group.getPrime('hex');
  const hash1 = hashes[name];
  const hash2 = crypto.createHash('sha1')
                    .update(private_key.toUpperCase()).digest('hex');
fork icon0
star icon0
watch icon0

37
38
39
40
41
42
43
44
45
46
.forEach((name) => {
  // modp1 is 768 bits, FIPS requires >= 1024
  if (name === 'modp1' && common.hasFipsCrypto)
    return;
  const group1 = crypto.getDiffieHellman(name);
  const group2 = crypto.getDiffieHellman(name);
  group1.generateKeys();
  group2.generateKeys();
  const key1 = group1.computeSecret(group2.getPublicKey());
  const key2 = group2.computeSecret(group1.getPublicKey());
fork icon0
star icon0
watch icon0

+ 3 other calls in file

9
10
11
12
13
14
15
16
17
18
t.plan(3)
var dh1 = cryptoB.getDiffieHellman(mod)
var p1 = dh1.getPrime().toString('hex')
dh1.generateKeys()

var dh2 = crypto.getDiffieHellman(mod)
var p2 = dh2.getPrime().toString('hex')
dh2.generateKeys()
t.equals(p1, p2, 'equal primes')
var pubk1 = dh1.getPublicKey()
fork icon0
star icon0
watch icon0