mirror of
https://github.com/crazy-max/ghaction-import-gpg.git
synced 2025-01-19 22:34:44 +01:00
e8a90f2bec
* Bump path-parse from 1.0.6 to 1.0.7 (#104) Bumps [path-parse](https://github.com/jbgutierrez/path-parse) from 1.0.6 to 1.0.7. - [Release notes](https://github.com/jbgutierrez/path-parse/releases) - [Commits](https://github.com/jbgutierrez/path-parse/commits/v1.0.7) --- updated-dependencies: - dependency-name: path-parse dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * OpenPGP.js 5.0.0 Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import * as openpgp from 'openpgp';
|
|
import addressparser from 'addressparser';
|
|
|
|
export interface PrivateKey {
|
|
fingerprint: string;
|
|
keyID: string;
|
|
name: string;
|
|
email: string;
|
|
creationTime: Date;
|
|
}
|
|
|
|
export interface KeyPair {
|
|
publicKey: string;
|
|
privateKey: string;
|
|
}
|
|
|
|
export const readPrivateKey = async (key: string): Promise<PrivateKey> => {
|
|
const privateKey = await openpgp.readKey({
|
|
armoredKey: (await isArmored(key)) ? key : Buffer.from(key, 'base64').toString()
|
|
});
|
|
|
|
const address = await privateKey.getPrimaryUser().then(primaryUser => {
|
|
return addressparser(primaryUser.user.userID?.userID)[0];
|
|
});
|
|
|
|
return {
|
|
fingerprint: privateKey.getFingerprint().toUpperCase(),
|
|
keyID: await privateKey.getEncryptionKey().then(encKey => {
|
|
// @ts-ignore
|
|
return encKey?.getKeyID().toHex().toUpperCase();
|
|
}),
|
|
name: address.name,
|
|
email: address.address,
|
|
creationTime: privateKey.getCreationTime()
|
|
};
|
|
};
|
|
|
|
export const generateKeyPair = async (name: string, email: string, passphrase: string, type?: 'ecc' | 'rsa'): Promise<KeyPair> => {
|
|
const keyPair = await openpgp.generateKey({
|
|
userIDs: [{name: name, email: email}],
|
|
passphrase: passphrase,
|
|
type: type
|
|
});
|
|
|
|
return {
|
|
publicKey: keyPair.publicKey.replace(/\r\n/g, '\n').trim(),
|
|
privateKey: keyPair.privateKey.replace(/\r\n/g, '\n').trim()
|
|
};
|
|
};
|
|
|
|
export const isArmored = async (text: string): Promise<boolean> => {
|
|
return text.trimLeft().startsWith('---');
|
|
};
|