MCPcopy Index your code
hub / github.com/nodejs/node / diffieHellman

Function diffieHellman

lib/internal/crypto/diffiehellman.js:288–370  ·  view source on GitHub ↗
(options, callback)

Source from the content-addressed store, hash-verified

286const dhEnabledKeyTypes = new SafeSet(['dh', 'ec', 'x448', 'x25519']);
287
288function diffieHellman(options, callback) {
289 validateObject(options, 'options');
290
291 if (callback !== undefined)
292 validateFunction(callback, 'callback');
293
294 const { privateKey, publicKey } = options;
295
296 // TODO(@panva): remove these non-semver-major error code preserving measures
297 // in a semver-major followup, the final state is just preparePublicOrPrivateKey
298 // and preparePrivateKey
299 if (privateKey == null)
300 throw new ERR_INVALID_ARG_VALUE('options.privateKey', privateKey);
301
302 if (publicKey == null)
303 throw new ERR_INVALID_ARG_VALUE('options.publicKey', publicKey);
304
305 if (isKeyObject(privateKey)) {
306 const type = getKeyObjectType(privateKey);
307 if (type !== 'private')
308 throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(type, 'private');
309 }
310
311 if (isKeyObject(publicKey)) {
312 const type = getKeyObjectType(publicKey);
313 if (type !== 'public' && type !== 'private') {
314 throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(type,
315 'private or public');
316 }
317 }
318
319 if (isKeyObject(privateKey) && isKeyObject(publicKey)) {
320 const privateType = getKeyObjectAsymmetricKeyType(privateKey);
321 const publicType = getKeyObjectAsymmetricKeyType(publicKey);
322 if (privateType !== publicType || !dhEnabledKeyTypes.has(privateType)) {
323 throw new ERR_CRYPTO_INCOMPATIBLE_KEY('key types for Diffie-Hellman',
324 `${privateType} and ${publicType}`);
325 }
326 }
327
328 const {
329 data: pubData,
330 format: pubFormat,
331 type: pubType,
332 passphrase: pubPassphrase,
333 namedCurve: pubNamedCurve,
334 } = preparePublicOrPrivateKey(publicKey, 'options.publicKey');
335
336 const {
337 data: privData,
338 format: privFormat,
339 type: privType,
340 passphrase: privPassphrase,
341 namedCurve: privNamedCurve,
342 } = preparePrivateKey(privateKey, 'options.privateKey');
343
344 const job = new DHBitsJob(
345 callback ? kCryptoJobAsync : kCryptoJobSync,

Calls 8

isKeyObjectFunction · 0.85
getKeyObjectTypeFunction · 0.85
preparePrivateKeyFunction · 0.85
hasMethod · 0.65
runMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…