Open In App

Node.js diffieHellman.verifyError Property

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

NodeJs Crypto module allows us to use cryptographic algorithms in order to secure confidential information. The Diffie-Hellman is also one of the cryptographic algorithms used for the secure sharing of the secret key between the two entities ( Sender/Receiver ). 

The Diffie-Hellman algorithm is based on a large prime number (P) and a generator ( G ). P’s primitive root is G.

Primitive root: Let G is the primitive root of prime P then – G is an integer between [ 1,  P – 1 ] and X is an integer between [ 1, P – 2 ] such that the value of  G X mod P  is different for each X value.

NodeJS diffieHellman.verifyError: A bitfield containing warnings or errors as a result of checks performed during initialization. This method indicates whether there is any error or not during the initialization of the Diffie-Hellman object. 

If there will be an error then the bit value will be according to the type of error otherwise it will be 0. 

The following are the properties that will decide the diffieHellman.verifyError value:

DH_CHECK_P_NOT_SAFE_PRIME: 2,
DH_CHECK_P_NOT_PRIME: 1,
DH_UNABLE_TO_CHECK_GENERATOR: 4,
DH_NOT_SUITABLE_GENERATOR: 8,
  • If the diffieHellman.verifyError method returns 2 means the specified prime number is not a safe prime.
  • If the diffieHellman.verifyError method returns 1 means the number is not prime. 
  • If the diffieHellman.verifyError method returns 4 means there is an error when reading the generator value.
  • If the diffieHellman.verifyError method returns 8 means the used generator is not suitable to generate Diffie-Hellman key object with the specified prime.

These numeric values are already defined for each type of error in crypto.constants object.

Let’s create a Diffie-Hellman object using the createDiffieHellman() method. The following is the syntax for creating an

Diffie-Hellman key exchange object:

crypto.createDiffieHellman( prime, primeEncoding,
    generator, generatorEncoding )

Parameters:

  • Prime: Can contain elements of type String, Buffer, TypedArray, or DataView.
  • primeEncoding: This is the prime string encoding and is of a type string.
  • Generator: Can contain Number, String, Buffer, TypedArray, or DataView data. The default value is 2.
  • generatorEncoding: This is the encoding of the generator string and returns a string.

Returns: Returns a Diffie-Hellman key exchange object.

The following is the syntax for diffieHellman.verifyError method:

diffieHellman_object_name.verifyError

Parameters: No parameters, It’s a property, not a function.

Returns: a number.

Example 1:  DH_CHECK_P_NOT_SAFE_PRIME : 2

In this example, we are creating a Diffie-Hellman object based on the prime number and generator values. In this case, the specified prime ( 61 ) is not a safe prime and we are trying to get the error value (2) corresponding to DH_CHECK_P_NOT_SAFE_PRIME key for this unsafe prime using the diffieHellman_object_name.verifyError property.

As we discussed if diffieHellman.verifyError method returns value 2, that indicates that the specified prime is not safe prime.

Javascript




// Importing the required Crypto module
const crypto = require('node:crypto');
 
// Creating the diffieHellman object -
    // createDiffieHellman( prime, generator )
const bob = crypto.createDiffieHellman('61',2);
 
// verifying the Error value -
console.log(bob.verifyError);


Output:

2

Example 2:  DH_CHECK_P_NOT_PRIME: 1

If the specified P is not an actual prime number then the property will return 1.

In this example, we are passing a number 82 to createDiffieHellman() method that is not a prime number. Our aim is to achieve the error value 1 corresponding to the DH_CHECK_P_NOT_PRIME key using the diffieHellman_object_name.verifyError property.

Javascript




// Importing the required Crypto module
const crypto = require('node:crypto');
 
// Creating the diffieHellman object -
    // createDiffieHellman( prime, generator )
const bob = crypto.createDiffieHellman('82',2);
 
// verifying the Error value -
console.log(bob.verifyError);


Output:

1

Example 3:  DH_NOT_SUITABLE_GENERATOR: 8,

In this example, we are passing 2 as the prime number and 50 as a generator. 50 is not a perfect generator match for prime 2 to create the Diffie-Hellman object. Our intention is to get the error value (8) corresponding to the DH_NOT_SUITABLE_GENERATOR key as 50 is not a suitable generator.

If the generator is not suitable for the specified prime and to generate Diffie-Hellman object the property will return 8. 

Javascript




// Importing the required Crypto module
const crypto = require('node:crypto');
 
// Creating the diffieHellman object -
    // createDiffieHellman( prime, generator )
const bob = crypto.createDiffieHellman(2,50);
 
// verifying the Error value -
console.log(bob.verifyError);


Output:

8

Example 4:  If there is no error – 0

In this example, Our aim is to get the error value (0). This 0 indicates that there is no error in the creation of the Diffie-Hellman object. 2 is the prime number and 7 is a suitable generator so the Diffie-Hellman object will be created without any error.

Javascript




// Importing the required Crypto module
const crypto = require('node:crypto');
 
// Creating the diffieHellman object -
    // createDiffieHellman( prime, generator )
const bob = crypto.createDiffieHellman(7,2);
 
// verifying the Error value -
console.log(bob.verifyError);


Output:

0

Reference: https://nodejs.org/api/crypto.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads