Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as openpgp from 'openpgp'
- type TUser = {
- user: {
- email: string;
- firstname: string;
- lastname: string;
- }
- };
- type TKeysArmored = {
- privateKeyArmored: string;
- publicKeyArmored: string;
- revocationCertificate?: string;
- };
- type TKeys = {
- privateKey: openpgp.PrivateKey;
- publicKey: openpgp.Key;
- };
- type TPassphrase = {
- passphrase: string;
- }
- type TEncryptDataParams = {
- data: string | number | Object;
- } & TUser & TPassphrase;
- type TEncryptedData = Omit<TKeysArmored, "revocationCertificate"> & {
- messageArmored: string;
- };
- type TDecryptDataParams = TEncryptedData & TPassphrase
- type TDecryptedData = {
- decryptedData: any;
- verifiedSignature: boolean;
- };
- type TReadKeysParams = TKeysArmored & TPassphrase;
- type TGeneratePgpKeysParams = TUser & TPassphrase
- interface IEncryptData {
- encryptData: (params: TEncryptDataParams) => Promise<TEncryptedData>;
- decryptData: (params: TDecryptDataParams) => Promise<TDecryptedData>;
- generatePgpKeys: (params: TGeneratePgpKeysParams) => Promise<TKeysArmored>;
- readKeys: (params: TReadKeysParams) => Promise<TKeys>;
- }
- class EncryptData implements IEncryptData {
- protected curve: openpgp.EllipticCurveName = 'curve25519';
- protected type: 'ecc' | 'rsa' = 'ecc';
- async decryptData({
- messageArmored,
- passphrase,
- privateKeyArmored,
- publicKeyArmored,
- }: TDecryptDataParams): Promise<TDecryptedData> {
- const { privateKey, publicKey } = await this.readKeys({
- passphrase,
- privateKeyArmored,
- publicKeyArmored,
- })
- const message = await openpgp.readMessage({
- armoredMessage: atob(messageArmored),
- });
- const { data: decryptedData, signatures } = await openpgp.decrypt({
- decryptionKeys: privateKey,
- message,
- verificationKeys: publicKey,
- });
- return {
- decryptedData,
- verifiedSignature: await signatures[0].verified,
- };
- }
- async encryptData({ data, passphrase, user }: TEncryptDataParams): Promise<TEncryptedData> {
- const { privateKeyArmored, publicKeyArmored } = await this.generatePgpKeys({
- passphrase,
- user
- });
- const { privateKey, publicKey } = await this.readKeys({
- passphrase,
- privateKeyArmored,
- publicKeyArmored,
- })
- const messageArmored = await openpgp.encrypt({
- encryptionKeys: publicKey,
- message: await openpgp.createMessage({
- text: data as any,
- }),
- signingKeys: privateKey,
- });
- return {
- publicKeyArmored,
- privateKeyArmored,
- messageArmored: btoa(messageArmored as string),
- };
- }
- async generatePgpKeys({ user, passphrase }: TGeneratePgpKeysParams): Promise<TKeysArmored> {
- const { email, firstname, lastname } = user;
- const {
- privateKey: privateKeyArmored,
- publicKey: publicKeyArmored,
- revocationCertificate,
- } = await openpgp.generateKey({
- curve: this.curve,
- passphrase,
- type: this.type,
- userIDs: [
- {
- email,
- name: `${firstname} ${lastname}`,
- },
- ],
- });
- return {
- privateKeyArmored,
- publicKeyArmored,
- revocationCertificate,
- };
- }
- async readKeys ({ passphrase, privateKeyArmored, publicKeyArmored }: TReadKeysParams): Promise<TKeys> {
- const publicKey = await openpgp.readKey({
- armoredKey: publicKeyArmored,
- });
- const privateKey = await openpgp.decryptKey({
- passphrase,
- privateKey: await openpgp.readPrivateKey({
- armoredKey: privateKeyArmored,
- }),
- });
- return { privateKey, publicKey };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement