當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js crypto.getCipherInfo()用法及代碼示例


node.js 中的 Node.js crypto.getCipherInfo() 方法提供了有關密碼的詳細信息。如果您想獲取有關模式、 key 長度、塊大小、初始化向量長度等不同參數的信息,此方法非常有用。

用法:以下是獲取有關密碼信息的語法:

// 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調用該方法。第二種方式是直接將方法導入到項目中。

參數:

  • 名稱<字符串>:這是要查詢的密碼的名稱。這可以作為字符串或數字傳遞。
  • 選項<對象>:該對象包含有關 key 長度 <number> 和 IV 長度 <number> 的信息。

返回值:該方法本質上返回一個對象,其中包含名稱、nid、塊大小、 key 長度和與密碼相關的模式等參數。

示例 1:在此示例中,我們定義了一個密碼 (aes-192-cbc) 並打印了與該密碼相關的詳細信息。我們使用 crypto.getCipherInfo() 方法來獲取詳細信息。

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

輸出:

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

示例 2:在此示例中,我們使用 crypto.getCiphers() 獲取所有密碼的列表,並從列表中選擇一個密碼並將其傳遞給 getCipherInfo(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);

輸出:

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



相關用法


注:本文由純淨天空篩選整理自harishcarpenter大神的英文原創作品 Node.js crypto.getCipherInfo() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。