How to use the createDiffieHellman function from crypto

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

25
26
27
28
29
30
31
32
33
34

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

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

    let clientPublicKey = dh.getPublicKey('hex')
    //console.log(clientPublicKey)
fork icon1
star icon0
watch icon0

+ 3 other calls in file

114
115
116
117
118
119
120
121
122
123
124
125
  '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

3
4
5
6
7
8
9
10
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

39
40
41
42
43
44
45
46
47
48
49
50
51


const p = crypto.createDiffieHellman(length).getPrime();


for (let i = 0; i < 2000; i++) {
  const a = crypto.createDiffieHellman(p);
  const b = crypto.createDiffieHellman(p);


  a.generateKeys();
  b.generateKeys();

fork icon0
star icon0
watch icon0

+ 5 other calls in file

27
28
29
30
31
32
33
34
35
36
37
38
const crypto = require('crypto');


function test() {
  const odd = Buffer.alloc(39, 'A');


  const c = crypto.createDiffieHellman(32);
  c.setPrivateKey(odd);
  c.generateKeys();
}

fork icon0
star icon0
watch icon0

46
47
48
49
50
51
52
53
54
55
56
57


// Through a fluke of history, g=0 defaults to DH_GENERATOR (2).
{
  const g = 0;
  crypto.createDiffieHellman('abcdef', g);
  crypto.createDiffieHellman('abcdef', 'hex', g);
}


for (const g of [-1, 1]) {
  const ex = {
fork icon0
star icon0
watch icon0

+ 139 other calls in file

9
10
11
12
13
14
15
16
17
18
19
const assert = require('assert');
const crypto = require('crypto');


const before = process.memoryUsage.rss();
{
  const dh = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256);
  const publicKey = dh.generateKeys();
  const privateKey = dh.getPrivateKey();
  for (let i = 0; i < 5e4; i += 1) {
    dh.setPublicKey(publicKey);
fork icon0
star icon0
watch icon0

582
583
584
585
586
587
588
589
590
591
// Test Diffie-Hellman with two parties sharing a secret,
// using various encodings as we go along
{
  const dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256);
  const p1 = dh1.getPrime('buffer');
  const dh2 = crypto.createDiffieHellman(p1, 'base64');
  const key1 = dh1.generateKeys();
  const key2 = dh2.generateKeys('hex');
  const secret1 = dh1.computeSecret(key2, 'hex', 'base64');
  const secret2 = dh2.computeSecret(key1, 'latin1', 'buffer');
fork icon0
star icon0
watch icon0

+ 7 other calls in file

94
95
96
97
98
99
100
101
102
103
104
105
dc8fe984ddaf532fc1531ce43155fa0ab32532bf1ece5356b8a3447b5267798a904f16f3f4e635\
597adc0179d011132dcffc0bbcb0dd2c8700872f8663ec7ddd897c659cc2efebccc73f38f0ec96\
8612314311231f905f91c63a1aea52e0b60cead8b57df';


/* FIPS-friendly 2048 bit prime */
const p = crypto.createDiffieHellman(
  crypto.getDiffieHellman('modp14').getPrime());


p.setPublicKey(apub, 'hex');
p.setPrivateKey(apriv, 'hex');
fork icon0
star icon0
watch icon0

+ 3 other calls in file

29
30
31
32
33
34
35
36
37
38
if (!CRYPTO_HAVE_ECDH) {
	throw (new Error('Due to bugs in the node 0.10 ' +
	    'crypto API, node 0.12.x or later is required ' +
	    'to use DH'));
}
this._dh = crypto.createDiffieHellman(
    key.part.p.data, undefined,
    key.part.g.data, undefined);
this._p = key.part.p;
this._g = key.part.g;
fork icon0
star icon0
watch icon0

32
33
34
35
36
37
38
39
40
41
t.test('' + len, function (t) {
  t.plan(3)
  var dh2 = cryptoB.createDiffieHellman(len)
  var prime2 = dh2.getPrime()
  var p2 = prime2.toString('hex')
  var dh1 = crypto.createDiffieHellman(prime2)
  var p1 = dh1.getPrime().toString('hex')
  dh1.generateKeys()
  dh2.generateKeys()
  t.equals(p1, p2, 'equal primes')
fork icon0
star icon0
watch icon0

383
384
385
386
387
388
389
390
391
392
a [`Buffer`][] is expected.

If `generator_encoding` is specified, `generator` is expected to be a string;
otherwise either a number or [`Buffer`][] is expected.

### crypto.createDiffieHellman(prime_length[, generator])

Creates a `DiffieHellman` key exchange object and generates a prime of
`prime_length` bits using an optional specific numeric `generator`.
If `generator` is not specified, the value `2` is used.
fork icon0
star icon0
watch icon1

+ 3 other calls in file