Node.js crypto.getCipherInfo() 方法

Node.js crypto.getCipherInfo() 方法

Node.js的 crypto.getCipherInfo() 方法提供了有关密码的详细信息。如果您想获取有关不同参数(如模式、密钥长度、块大小、初始化向量长度等)的信息,此方法非常有用。

语法: 以下是获取密码信息的语法:

// 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

如您在上面的语法中所看到的,有两种方法可以在我们的项目中使用这个方法。首先,您可以在项目中导入crypto模块,并通过crypto调用该方法。第二种方式是直接将方法导入项目中。

参数:

  • Name <string>: 这是要查询的密码器的名称。可以传递为字符串或数字。
  • Options <object>: 这个对象包含有关密钥长度<number>和IV长度<number>的信息。

    返回值: 此方法基本上返回一个对象,其中包含与密码器相关的参数,如名称、nid、块大小、密钥长度和模式。

示例1: 在这个示例中,我们定义了一个密码器(aes-192-cbc)并打印了与此密码器相关的详细信息。我们使用crypto.getCipherInfo()方法来获取有关详细信息。

// 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);

输出:

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

示例2: 在这个示例中,我们使用crypto.getCiphers()来获取所有密码的列表,并从列表中选择一个密码并将其传递给getCipherInfo(cipher)来获取有关此密码的信息。

// 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);

输出:

[
    ...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
}

参考资料: https://nodejs.org/api/crypto.html#cryptogetcipherinfonameornid-options

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程