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
相关用法
- Node.js crypto.getCiphers()用法及代码示例
- Node.js crypto.getCurves()用法及代码示例
- Node.js crypto.getHashes()用法及代码示例
- Node.js crypto.getDiffieHellman()用法及代码示例
- Node.js crypto.getDiffieHellman(groupName)用法及代码示例
- Node.js crypto.generateKeyPair()用法及代码示例
- Node.js crypto.generateKeyPairSync()用法及代码示例
- Node.js crypto.generateKey(type, options, callback)用法及代码示例
- Node.js crypto.generateKeyPair(type, options, callback)用法及代码示例
- Node.js crypto.generateKeyPairSync(type, options)用法及代码示例
- Node.js crypto.generateKeySync(type, options)用法及代码示例
- Node.js crypto.createDiffieHellman()用法及代码示例
- Node.js crypto.createSign()用法及代码示例
- Node.js crypto.createVerify()用法及代码示例
- Node.js crypto.pbkdf2()用法及代码示例
- Node.js crypto.createHash()用法及代码示例
- Node.js crypto.createHmac()用法及代码示例
- Node.js crypto.randomBytes()用法及代码示例
- Node.js crypto.publicDecrypt()用法及代码示例
- Node.js crypto.pbkdf2Sync()用法及代码示例
- Node.js crypto.createECDH()用法及代码示例
- Node.js crypto.createDecipheriv()用法及代码示例
- Node.js crypto.createCipheriv()用法及代码示例
- Node.js crypto.createDiffieHellmanGroup()用法及代码示例
- Node.js crypto.privateDecrypt()用法及代码示例
注:本文由纯净天空筛选整理自harishcarpenter大神的英文原创作品 Node.js crypto.getCipherInfo() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
