Advertisement
baxterio

openpgp curve25519 implementation

Dec 30th, 2023
1,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 3.66 KB | Cybersecurity | 0 0
  1. import * as openpgp from 'openpgp'
  2.  
  3. type TUser = {
  4.   user: {
  5.     email: string;
  6.     firstname: string;
  7.     lastname: string;
  8.   }
  9. };
  10.  
  11. type TKeysArmored = {
  12.   privateKeyArmored: string;
  13.   publicKeyArmored: string;
  14.   revocationCertificate?: string;
  15. };
  16.  
  17. type TKeys = {
  18.   privateKey: openpgp.PrivateKey;
  19.   publicKey: openpgp.Key;
  20. };
  21.  
  22. type TPassphrase = {
  23.   passphrase: string;
  24. }
  25.  
  26. type TEncryptDataParams = {
  27.   data: string | number | Object;
  28. } & TUser & TPassphrase;
  29.  
  30. type TEncryptedData = Omit<TKeysArmored, "revocationCertificate"> & {
  31.   messageArmored: string;
  32. };
  33.  
  34. type TDecryptDataParams = TEncryptedData & TPassphrase
  35.  
  36. type TDecryptedData = {
  37.   decryptedData: any;
  38.   verifiedSignature: boolean;
  39. };
  40.  
  41. type TReadKeysParams = TKeysArmored & TPassphrase;
  42.  
  43. type TGeneratePgpKeysParams = TUser & TPassphrase
  44.  
  45. interface IEncryptData {
  46.   encryptData: (params: TEncryptDataParams) => Promise<TEncryptedData>;
  47.   decryptData: (params: TDecryptDataParams) => Promise<TDecryptedData>;
  48.   generatePgpKeys: (params: TGeneratePgpKeysParams) => Promise<TKeysArmored>;
  49.   readKeys: (params: TReadKeysParams) => Promise<TKeys>;
  50. }
  51.  
  52. class EncryptData implements IEncryptData {
  53.   protected curve: openpgp.EllipticCurveName = 'curve25519';
  54.   protected type: 'ecc' | 'rsa' = 'ecc';
  55.  
  56.   async decryptData({
  57.     messageArmored,
  58.     passphrase,
  59.     privateKeyArmored,
  60.     publicKeyArmored,
  61.   }: TDecryptDataParams): Promise<TDecryptedData> {
  62.     const { privateKey, publicKey } = await this.readKeys({
  63.       passphrase,
  64.       privateKeyArmored,
  65.       publicKeyArmored,
  66.     })
  67.  
  68.     const message = await openpgp.readMessage({
  69.       armoredMessage: atob(messageArmored),
  70.     });
  71.  
  72.     const { data: decryptedData, signatures } = await openpgp.decrypt({
  73.       decryptionKeys: privateKey,
  74.       message,
  75.       verificationKeys: publicKey,
  76.     });
  77.  
  78.     return {
  79.       decryptedData,
  80.       verifiedSignature: await signatures[0].verified,
  81.     };
  82.   }
  83.  
  84.   async encryptData({ data, passphrase, user }: TEncryptDataParams): Promise<TEncryptedData> {
  85.     const { privateKeyArmored, publicKeyArmored } = await this.generatePgpKeys({
  86.       passphrase,
  87.       user
  88.     });
  89.  
  90.     const { privateKey, publicKey } = await this.readKeys({
  91.       passphrase,
  92.       privateKeyArmored,
  93.       publicKeyArmored,
  94.     })
  95.  
  96.     const messageArmored = await openpgp.encrypt({
  97.       encryptionKeys: publicKey,
  98.       message: await openpgp.createMessage({
  99.         text: data as any,
  100.       }),
  101.       signingKeys: privateKey,
  102.     });
  103.  
  104.     return {
  105.       publicKeyArmored,
  106.       privateKeyArmored,
  107.       messageArmored: btoa(messageArmored as string),
  108.     };
  109.   }
  110.  
  111.   async generatePgpKeys({ user, passphrase }: TGeneratePgpKeysParams): Promise<TKeysArmored> {
  112.     const { email, firstname, lastname  } = user;
  113.  
  114.     const {
  115.       privateKey: privateKeyArmored,
  116.       publicKey: publicKeyArmored,
  117.       revocationCertificate,
  118.     } = await openpgp.generateKey({
  119.       curve: this.curve,
  120.       passphrase,
  121.       type: this.type,
  122.       userIDs: [
  123.         {
  124.           email,
  125.           name: `${firstname} ${lastname}`,
  126.         },
  127.       ],
  128.     });
  129.  
  130.     return {
  131.       privateKeyArmored,
  132.       publicKeyArmored,
  133.       revocationCertificate,
  134.     };
  135.   }
  136.  
  137.   async readKeys ({ passphrase, privateKeyArmored, publicKeyArmored }: TReadKeysParams): Promise<TKeys> {
  138.     const publicKey = await openpgp.readKey({
  139.       armoredKey: publicKeyArmored,
  140.     });
  141.  
  142.     const privateKey = await openpgp.decryptKey({
  143.       passphrase,
  144.       privateKey: await openpgp.readPrivateKey({
  145.         armoredKey: privateKeyArmored,
  146.       }),
  147.     });
  148.  
  149.     return { privateKey, publicKey };
  150.   }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement