mirror of
https://github.com/crazy-max/ghaction-import-gpg.git
synced 2025-02-23 23:30:59 -05:00
54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
|
import * as openpgp from 'openpgp';
|
||
|
|
||
|
export interface PrivateKey {
|
||
|
fingerprint: string;
|
||
|
keyID: string;
|
||
|
userID: string;
|
||
|
creationTime: Date;
|
||
|
}
|
||
|
|
||
|
export interface KeyPair {
|
||
|
publicKey: string;
|
||
|
privateKey: string;
|
||
|
}
|
||
|
|
||
|
export const readPrivateKey = async (armoredText: string): Promise<PrivateKey> => {
|
||
|
const {
|
||
|
keys: [privateKey],
|
||
|
err: err
|
||
|
} = await openpgp.key.readArmored(armoredText);
|
||
|
if (err?.length) {
|
||
|
throw err[0];
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
fingerprint: privateKey.getFingerprint().toUpperCase(),
|
||
|
keyID: await privateKey.getEncryptionKey().then(encKey => {
|
||
|
// @ts-ignore
|
||
|
return encKey?.getKeyId().toHex().toUpperCase();
|
||
|
}),
|
||
|
userID: await privateKey.getPrimaryUser().then(primaryUser => {
|
||
|
return primaryUser.user.userId.userid;
|
||
|
}),
|
||
|
creationTime: privateKey.getCreationTime()
|
||
|
};
|
||
|
};
|
||
|
|
||
|
export const generateKeyPair = async (
|
||
|
name: string,
|
||
|
email: string,
|
||
|
passphrase: string,
|
||
|
numBits: number = 4096
|
||
|
): Promise<KeyPair> => {
|
||
|
const keyPair = await openpgp.generateKey({
|
||
|
userIds: [{name: name, email: email}],
|
||
|
numBits,
|
||
|
passphrase
|
||
|
});
|
||
|
|
||
|
return {
|
||
|
publicKey: keyPair.publicKeyArmored.replace(/\r\n/g, '\n').trim(),
|
||
|
privateKey: keyPair.privateKeyArmored.replace(/\r\n/g, '\n').trim()
|
||
|
};
|
||
|
};
|