Jod*_*odo 6 openssh rsa node.js key-pair
I am on node version: v10.14.1 and I generate keyPairs with this code:
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: ''
}
}, (err, publicKey, privateKey) => {
// Do stuff
});
Run Code Online (Sandbox Code Playgroud)
This will create a public key in this format:
-----BEGIN RSA PUBLIC KEY-----
...
-----END RSA PUBLIC KEY-----
Run Code Online (Sandbox Code Playgroud)
Unfortunately sometimes different formats are needed. In my case to upload public key to AWS the OpenSSH format is needed which I believe is something like this:
ssh-rsa
...
Run Code Online (Sandbox Code Playgroud)
How can I either convert the RSA public key format to OpenSSH format or generate it directly with generateKeyPair()?
node-sshpk 包可能会帮助你:https : //github.com/joyent/node-sshpk
您可以使用pubKey.toBuffer()或者更复杂一点的pubKey.toBuffer('ssh'). 或者pubKey.toString('ssh')如果你需要它作为一个字符串。
在您的示例中,代码应该是这样的:
const { generateKeyPair } = require('crypto');
const sshpk = require('sshpk');
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
}
}, (err, publicKey, privateKey) => {
if(err){
// handle Error
}
else{
const pemKey = sshpk.parseKey(publicKey, 'pem');
const sshRsa = pemKey.toString('ssh');
console.log(ssh_rsa_2);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1674 次 |
| 最近记录: |