Last active
September 23, 2019 08:29
-
-
Save joelharkes/efd7f89f43f088304c2af219c993ed61 to your computer and use it in GitHub Desktop.
Generate Pgp keys with node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]" }], // 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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