Skip to content

Instantly share code, notes, and snippets.

@joelharkes
Last active September 23, 2019 08:29
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 joelharkes/efd7f89f43f088304c2af219c993ed61 to your computer and use it in GitHub Desktop.
Save joelharkes/efd7f89f43f088304c2af219c993ed61 to your computer and use it in GitHub Desktop.
Generate Pgp keys with node
import * as openpgp from "openpgp";
import { writeFileSync } from "fs";
import { join } from "path";
var phrase = "test12";
generateRsaKeys('test', phrase);
async function generateRsaKeys(name: string, passphrase: string) {
var options: openpgp.KeyOptions = {
userIds: [{ name: "Test user", email: "test@user.email" }], // multiple user IDs
// curve: "ed25519", // ECC curve name, un comment to use curve, much faster generation, but not all tooling supports it.
passphrase: phrase, // protects the private key
numBits: 4096,
};
var key = await openpgp.generateKey(
options
);
writeFileSync(join(__dirname, name + ".private.pgp"), key.privateKeyArmored);
writeFileSync(join(__dirname, name + ".public.pgp"), key.publicKeyArmored);
}
import * as openpgp from "openpgp";
import { readFileSync } from "fs";
import { join } from "path";
var phrase = "test12";
run();
async function run() {
var privateKey = readFileSync(join(__dirname, "private.pgp"));
var publicKey = readFileSync(join(__dirname, "public.pgp"));
const privKeyObj = (await openpgp.key.readArmored(privateKey)).keys[0];
await privKeyObj.decrypt(phrase);
var optionsX = {
message: openpgp.message.fromText("Hello world!"), // input as Message object
publicKeys: (await openpgp.key.readArmored(publicKey)).keys, // for encryption
privateKeys: [privKeyObj], // for signing (optional)
};
const encrypted = await openpgp.encrypt(optionsX);
const ciphertext = encrypted.data;
var optionsY = {
message: await openpgp.message.readArmored(ciphertext), // parse armored message
publicKeys: (await openpgp.key.readArmored(publicKey)).keys, // for verification (optional)
privateKeys: [privKeyObj], // for decryption
};
const decrypted = await openpgp.decrypt(optionsY);
var x = decrypted.data;
console.log(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment