crypto.generateKey(type, options, callback)
历史
| 版本 | 变化 | 
|---|---|
| v18.0.0 | 将无效回调传递给   | 
| v15.0.0 | 添加于:v15.0.0  | 
参数
type: <string> 生成的 key 的预期用途。当前接受的值为'hmac'和'aes'。options:<Object>length:<number>要生成的 key 的位长。这必须是一个大于 0 的值。- 如果 
type为'hmac',则最小值为 1,最大长度为 231-1。如果该值不是 8 的倍数,则生成的 key 将被截断为Math.floor(length / 8)。 - 如果 
type是'aes',则长度必须是128、192或256之一。 
- 如果 
 
callback:<Function>err: <Error>key: <KeyObject>
异步生成给定 length 的新随机 key 。 type 将确定将在 length 上执行哪些验证。
const { generateKey } = await import('node:crypto'); generateKey('hmac', { length: 64 }, (err, key) => { if (err) throw err; console.log(key.export().toString('hex')); // 46e..........620 });const { generateKey, } = require('node:crypto'); generateKey('hmac', { length: 64 }, (err, key) => { if (err) throw err; console.log(key.export().toString('hex')); // 46e..........620 });
相关用法
- Node.js crypto.generateKeyPairSync(type, options)用法及代码示例
 - Node.js crypto.generateKeyPair()用法及代码示例
 - Node.js crypto.generateKeyPair(type, options, callback)用法及代码示例
 - Node.js crypto.generateKeyPairSync()用法及代码示例
 - Node.js crypto.generateKeySync(type, options)用法及代码示例
 - Node.js crypto.getCurves()用法及代码示例
 - Node.js crypto.getHashes()用法及代码示例
 - Node.js crypto.getDiffieHellman(groupName)用法及代码示例
 - Node.js crypto.getCiphers()用法及代码示例
 - Node.js crypto.getDiffieHellman()用法及代码示例
 - Node.js crypto.randomFill()用法及代码示例
 - Node.js crypto.createHmac()用法及代码示例
 - Node.js crypto.randomFillSync(buffer[, offset][, size])用法及代码示例
 - Node.js crypto.constants用法及代码示例
 - Node.js crypto.randomInt([min, ]max[, callback])用法及代码示例
 - Node.js crypto.publicEncrypt()用法及代码示例
 - Node.js crypto.publicDecrypt()用法及代码示例
 - Node.js crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)用法及代码示例
 - Node.js crypto.createHash()用法及代码示例
 - Node.js crypto.hkdfSync()用法及代码示例
 - Node.js crypto.randomFillSync()用法及代码示例
 - Node.js crypto.checkPrime()用法及代码示例
 - Node.js crypto.sign()用法及代码示例
 - Node.js crypto.webcrypto用法及代码示例
 - Node.js crypto.createCipheriv()用法及代码示例
 
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 crypto.generateKey(type, options, callback)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
