Skip to content

Instantly share code, notes, and snippets.

@rigwild
Created February 17, 2024 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rigwild/b497d430fd9e7c3e64c1eae4713ae05d to your computer and use it in GitHub Desktop.
Save rigwild/b497d430fd9e7c3e64c1eae4713ae05d to your computer and use it in GitHub Desktop.
Super simple helper class to use OpenPGP.js without getting an headache

PgpHelper

Super simple helper class to use OpenPGP.js without getting an headache

See https://github.com/openpgpjs/openpgpjs

Install dependencies

npm i openpgp

Usage

const PGP_PUBLIC_KEY = `
-----BEGIN PGP PUBLIC KEY BLOCK-----

mDMEZdEkQRYJKwYBBAHaRw8BAQdAIMC5PWpg5s7CsY38SfXx4LVao3NNQ1MeLwun
ye3JU9a0DEdpc3QgZXhhbXBsZYiZBBMWCgBBFiEE/ARfXsVven+e1YgtIoXAaZdb
GokFAmXRJEECGwMFCQPDJe8FCwkIBwICIgIGFQoJCAsCBBYCAwECHgcCF4AACgkQ
IoXAaZdbGol4uwD/WXCv2gj54RyaWy55Ck+XA/WSltO9hWr4BiHdagR/8eMBAJYz
bxqh9FOQI7EeEEwUDQpskAg9E17n1w7aKqoAwM0NuDgEZdEkQRIKKwYBBAGXVQEF
AQEHQEniNQMH2rM7LAle1LKWXIC7SBpYlkCn01BQfMuKJuhNAwEIB4h+BBgWCgAm
FiEE/ARfXsVven+e1YgtIoXAaZdbGokFAmXRJEECGwwFCQPDJe8ACgkQIoXAaZdb
GoklYAEAqZLgUUzZt0T0A153sT+UC5gWaQInDR6DaGVmfoH1Na4BAPUzOWgb1U8K
MmQHOfm5cBuK2iaTm07enRUSLEx9LGkJ
=nILo
-----END PGP PUBLIC KEY BLOCK-----
`

const PGP_PRIVATE_KEY = `
-----BEGIN PGP PRIVATE KEY BLOCK-----

lFgEZdEkQRYJKwYBBAHaRw8BAQdAIMC5PWpg5s7CsY38SfXx4LVao3NNQ1MeLwun
ye3JU9YAAQCDkLuaJI3AN9WK+DpC0pnwJoTmz/S8RRgU20B1Yth7EhEWtAxHaXN0
IGV4YW1wbGWImQQTFgoAQRYhBPwEX17Fb3p/ntWILSKFwGmXWxqJBQJl0SRBAhsD
BQkDwyXvBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4HAheAAAoJECKFwGmXWxqJeLsA
/1lwr9oI+eEcmlsueQpPlwP1kpbTvYVq+AYh3WoEf/HjAQCWM28aofRTkCOxHhBM
FA0KbJAIPRNe59cO2iqqAMDNDZxdBGXRJEESCisGAQQBl1UBBQEBB0BJ4jUDB9qz
OywJXtSyllyAu0gaWJZAp9NQUHzLiiboTQMBCAcAAP9tvWqsrNRaErTAOdfJeIzO
QtQUM1GEXgJkH6VfvP+LcBEZiH4EGBYKACYWIQT8BF9exW96f57ViC0ihcBpl1sa
iQUCZdEkQQIbDAUJA8Ml7wAKCRAihcBpl1saiSVgAQCpkuBRTNm3RPQDXnexP5QL
mBZpAicNHoNoZWZ+gfU1rgEA9TM5aBvVTwoyZAc5+blwG4raJpObTt6dFRIsTH0s
aQk=
=FDFL
-----END PGP PRIVATE KEY BLOCK-----
`

const text = "hello, i'm rigwild! :)!"
const textEncrypted = await PgpHelper.encryptTextWithPGP(text, PGP_PUBLIC_KEY)
console.log('Encrypted text:\n', textEncrypted)
/* Encrypted text:
-----BEGIN PGP MESSAGE-----

wV4DkFSFbular3sSAQdAuh6k+UkqQQJiooGoJALdNk6wA4z/g0g2cXDGybiS
+mswhyclZ9dc5yppHr4mVWt3HazuaBVm2FwCuPynHkVxtftKTWQ0RTcdHZRM
6zfMo8AX0kcBnH+appSw2TxEnhbTsc5+fxtvaDdi0L4Wd2+BpMfinjN948l6
HzG9DfWcdUqu84a4VmDE9wx94bw/DPhI4NtC1QmxnsO0/w==
=gmPz
-----END PGP MESSAGE-----
*/

const textDecrypted = await PgpHelper.decryptTextWithPGP(textEncrypted, PGP_PRIVATE_KEY)
console.log('Decrypted text:', textDecrypted)
/* Decrypted text: hello, i'm rigwild! :) */
import * as openpgp from 'openpgp'
// or
// import * as openpgp from 'https://unpkg.com/openpgp@5.11.0/dist/openpgp.min.mjs'
export class PgpHelper {
/**
* @param {string} text
* @param {string} pgpPublicKey
*/
static async encryptTextWithPGP(text, pgpPublicKey) {
const publicKey = await openpgp.readKey({ armoredKey: pgpPublicKey })
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text }),
encryptionKeys: publicKey,
})
return /** @type {string} */ (encrypted)
}
/**
* @param {any} data
* @param {string} pgpPublicKey
*/
static async encryptFileWithPGP(data, pgpPublicKey) {
const publicKey = await openpgp.readKey({ armoredKey: pgpPublicKey })
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ binary: new Uint8Array(data) }),
encryptionKeys: publicKey,
})
return encrypted
}
/**
* @param {string} encryptedText
* @param {string} privateKeyArmored
*/
static async decryptTextWithPGP(encryptedText, privateKeyArmored) {
const decrypted = await this.#decryptWithPGP(encryptedText, privateKeyArmored)
return /** @type {string} */ (decrypted)
}
/**
* @param {any} encryptedData
* @param {string} privateKeyArmored
*/
static async decryptFileWithPGP(encryptedData, privateKeyArmored) {
return this.#decryptWithPGP(encryptedData, privateKeyArmored, 'binary')
}
/**
* @param {any} encryptedData
* @param {string} privateKeyArmored
* @param {'utf8' | 'binary'} format
*/
static async #decryptWithPGP(encryptedData, privateKeyArmored, format = 'utf8') {
const privateKey = await openpgp.readPrivateKey({ armoredKey: privateKeyArmored })
const message = await openpgp.readMessage({
armoredMessage: encryptedData,
})
return openpgp
.decrypt({
message,
decryptionKeys: privateKey,
format,
})
.then(res => res.data)
}
}
import * as openpgp from 'openpgp';
// or
// import * as openpgp from 'https://unpkg.com/openpgp@5.11.0/dist/openpgp.min.mjs';
export class PgpHelper {
static async encryptTextWithPGP(text: string, pgpPublicKey: string): Promise<string> {
const publicKey = await openpgp.readKey({ armoredKey: pgpPublicKey });
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text }),
encryptionKeys: publicKey,
});
return encrypted as string;
}
static async encryptFileWithPGP(data: ArrayBuffer, pgpPublicKey: string): Promise<string> {
const publicKey = await openpgp.readKey({ armoredKey: pgpPublicKey });
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ binary: new Uint8Array(data) }),
encryptionKeys: publicKey,
});
return encrypted;
}
static async decryptTextWithPGP(encryptedText: string, privateKeyArmored: string): Promise<string> {
const decrypted = await this.#decryptWithPGP(encryptedText, privateKeyArmored);
return decrypted as string;
}
static async decryptFileWithPGP(encryptedData: string, privateKeyArmored: string): Promise<Uint8Array> {
return this.#decryptWithPGP(encryptedData, privateKeyArmored, 'binary') as Promise<Uint8Array>;
}
private static async #decryptWithPGP(encryptedData: string, privateKeyArmored: string, format: 'utf8' | 'binary' = 'utf8'): Promise<string | Uint8Array> {
const privateKey = await openpgp.readPrivateKey({ armoredKey: privateKeyArmored });
const message = await openpgp.readMessage({ armoredMessage: encryptedData });
const decrypted = await openpgp.decrypt({
message,
decryptionKeys: privateKey,
format,
});
return decrypted.data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment