import-gpg/src/openpgp.ts
CrazyMax e8a90f2bec
OpenPGP.js v5 (#78)
* 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>
2021-09-05 01:00:24 +02:00

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('---');
};