Open In App

Node.js crypto.getCipherInfo() Method

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js crypto.getCipherInfo() method in node.js provides detailed information about the cipher. This method is very useful in case you want to get information about the different parameters like mode, key length, block size, initialization vector length, etc.

Syntax: The following is the syntax for getting information about ciphers:

// Import crypto module
const crypto = require('crypto');

// Call this method through crypto class object
crypto.getCipherInfo(name[, options);
or
const { getCipherInfo } = require('node:crypto'); 

// Directly import this method in project.
getCipherInfor(); // Calls directly

As you can see in the above syntax there are two ways to use this method in our project. Firstly you can import the crypto module in the project and through crypto, call the method. the second way is to directly import the method into the project.

 

Parameters:

  • Name <string>:  This is the name of the cipher to query. This can be passed as a string or a number.
  • Options <object>:  This object contains information about Key length <number> and IV length <number>. 

Return Value: This method basically returns an object which consists of the parameters like name, nid, block size, key length, and mode related to cipher.

Example 1: In this example, we have defined a cipher (aes-192-cbc)  and printed the details related to this cipher. we have used crypto.getCipherInfo() method to get the details about the details.

Javascript




// Node.js program to demonstrate the
// crypto.getCipherInfo() method
  
// Importing the method from crypto module
const { getCipherInfo } = require('node:crypto');
  
// Defining our Cipher as a string
const cipher = 'aes-192-cbc';
  
// Storing the object returned by the below
// method into details variable
details = getCipherInfo(cipher);
  
// Printing the details
console.log(details);


Output:

{
    mode: 'cbc',
    name: 'aes-192-cbc'
    nid: 423,
    blockSize: 16,
    ivLength: 16,
    keyLength: 24
}

Example 2: In this example, we have used crypto.getCiphers() to get the list of all ciphers and from the list, we have selected one cipher and passed it to getCipherInfo(cipher) to get the information about this cipher.

Javascript




// Node.js program to demonstrate the
// crypto.getCipherInfo() method
  
// Importing the crypto module
const crypto = require('node:crypto');
  
// Printing the list of ciphers
console.log(crypto.getCiphers());
  
// Defining our Cipher as a string
const cipher = 'aes-128-cbc-hmac-sha1';
  
// Storing the object returned by the
// below method into details variable
details = crypto.getCipherInfo(cipher);
  
// Printing the details
console.log(details);


Output:

[
    ...aria-192-cfb1',
    'aria-192-cfb8',
    'aria-192-ctr',
    'aria-192-ecb',
    'aria-192-gcm',
    'aria-192-ofb',
    'aria-256-cbc',
    'aria-256-ccm',
    'aria-256-cfb',
    'aria-256-cfb1',
    'aria-256-cfb8',
    'aria-256-ctr',
    'aria-256-ecb',
    'aria-256-gcm',
    'aria-256-ofb'. 
    'aria128',
    'aria192',
    'aria256',
    'bf',
    'bf-cbc',
    'bf-cfb',
    'bf-ecb',
    'bf-ofb',
    'blowfish',
    'camellia-128-cbc', 
    'camellia-128-cfb',
    'camellia-128-cfb1',
    'camellia-128-cfb8',
    'camellia-128-ctr',
    'camellia-128-ecb',
    'camellia-128-ofb',
    'camellia-192-cbc',
    'camellia-192-cfb',
    'camellia-192-cfb1',
    'camellia-192-cfb8',
    'camellia-192-ctr',
    'camellia-192-ecb',
    'camellia-192-ofb', 
    'camellia-256-cbc',
    'camellia-256-cfb',
    'camellia-256-cfb1',
    'camellia-256-cfb8',
    'camellia-256-ctr',
    'camellia-256-ecb',
    'camellia-256-ofb',
    'camellia128',
    ...75 more items
]
{
    mode: 'cbc',
    name: 'aes-128-cbc-hmac-sha1',
    nid: 916,
    blockSize: 16,
    ivLength: 16,
    keyLength: 16
}

Reference: https://nodejs.org/api/crypto.html#cryptogetcipherinfonameornid-options



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads