如何在Node.js中使用加密生成多对密钥
在node.js中,有这段代码可以生成配对。但是如果我将名称更改为{key, public_key}
,然后尝试console.log
,则会显示undefined
。但是,我需要两对,不能用相同的名称运行两次,否则它会告诉它们之前已定义。还有另一种方法可以用加密生成另一对吗?
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
键:未定义 pub_key:未定义
或:
{ privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
^
SyntaxError: Unexpected token =
答案 0 :(得分:0)
您不能在一行中同时使用两个接收器功能,因为我们必须等待两者同时出现然后再打印。因此,最好使用回调函数来获取答案并放入外部变量
<td> <input style="width:70px" type="text" class="form-control" name="College_name[]" value="" id=""></td>
答案 1 :(得分:0)
您可以在破坏性分配过程中将publicKey, privateKey
属性分配给different variables,如下所示:
const { generateKeyPairSync } = require('crypto');
const keyOptions = [{
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret 1'
}
}, {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret 2'
}
}]
const [
{ publicKey: publicKey1, privateKey: privateKey1 },
{ publicKey: publicKey2, privateKey: privateKey2 }
] = keyOptions.map(options => generateKeyPairSync('rsa', options))
console.log(
publicKey1,
privateKey1,
publicKey2,
privateKey2
)