it-tools/src/tools/ecdsa-key-pair-generator/ecdsa-key-pair-generator.service.ts
2024-05-05 20:02:18 +02:00

34 lines
857 B
TypeScript

import sshpk from 'sshpk';
export { generateKeyPair };
async function generateKeyPair(config: {
password?: string
format?: sshpk.PrivateKeyFormatType
curve?: sshpk.CurveType
comment?: string
} = {}) {
const privKey = sshpk.generatePrivateKey('ecdsa', {
curve: config?.curve,
});
privKey.comment = config?.comment;
const pubFormat = config.format ?? 'ssh';
let privFormat: sshpk.PrivateKeyFormatType = config.format ?? 'ssh';
if (privFormat === 'ssh') {
privFormat = 'ssh-private';
}
const pubKey = privKey.toPublic();
return {
publicKey: pubKey.toString(pubFormat),
privateKey: config?.password
? privKey.toString(privFormat,
{
passphrase: config?.password,
comment: config?.comment,
},
)
: privKey.toString(privFormat, { comment: config?.comment }),
};
}