openpgp encrypt, sign, decrypt and verify: working versions (tested 2026-10-16)
Generate a passphrase protected curve25519 OpenPGP key set and exercise encrypt and sign, decrypt and verify, detached signatures, and wrong key failures offline with openpgp v6.
passed4 passed · 0 failed2026-10-16 20:33 UTC- Status
- passed
- Last passed
- 2026-10-16 20:33 UTC
- Tests
- 4 / 4
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
openpgp@6.3.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38962988948
The code
Exactly the files that passed. Your agent can pull them with get_recipe("openpgp-encrypt-decrypt") (install), or see them on GitHub.
package.json
{
"name": "openpgp-encrypt-decrypt",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"openpgp": "6.3.2"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/pgp.ts
import * as openpgp from "openpgp";
import type { PrivateKey } from "openpgp";
export type KeyPair = {
publicKey: string;
privateKey: string;
};
export type DecryptResult = {
text: string;
verified: boolean;
};
export async function generateKeyPair(passphrase: string, name: string, email: string): Promise<KeyPair> {
const { privateKey, publicKey } = await openpgp.generateKey({
type: "ecc",
curve: "curve25519Legacy",
userIDs: [{ name, email }],
passphrase,
format: "armored",
});
return { publicKey, privateKey };
}
export async function unlockPrivateKey(armoredKey: string, passphrase: string): Promise<PrivateKey> {
const privateKey = await openpgp.readPrivateKey({ armoredKey });
return openpgp.decryptKey({ privateKey, passphrase });
}
export async function encryptAndSign(text: string, publicKeyArmored: string, signingKey: PrivateKey): Promise<string> {
const message = await openpgp.createMessage({ text });
const encryptionKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
const armored = await openpgp.encrypt({
message,
encryptionKeys: encryptionKey,
signingKeys: signingKey,
});
return armored as string;
}
export async function decryptAndVerify(
armoredMessage: string,
decryptionKey: PrivateKey,
verificationKeyArmored: string,
): Promise<DecryptResult> {
const message = await openpgp.readMessage({ armoredMessage });
const verificationKey = await openpgp.readKey({ armoredKey: verificationKeyArmored });
const { data, signatures } = await openpgp.decrypt({
message,
decryptionKeys: decryptionKey,
verificationKeys: verificationKey,
});
const verified = signatures.length > 0 && (await signatures[0].verified);
return { text: data as string, verified };
}
export async function signDetached(text: string, signingKey: PrivateKey): Promise<string> {
const message = await openpgp.createMessage({ text });
const armored = await openpgp.sign({
message,
signingKeys: signingKey,
detached: true,
format: "armored",
});
return armored as string;
}
export async function verifyDetached(
text: string,
armoredSignature: string,
verificationKeyArmored: string,
): Promise<boolean> {
const message = await openpgp.createMessage({ text });
const signature = await openpgp.readSignature({ armoredSignature });
const verificationKey = await openpgp.readKey({ armoredKey: verificationKeyArmored });
const { signatures } = await openpgp.verify({
message,
signature,
verificationKeys: verificationKey,
});
return signatures.length > 0 && (await signatures[0].verified);
}
test/pgp.test.ts
import { test, before } from "node:test";
import assert from "node:assert/strict";
import type { PrivateKey } from "openpgp";
import {
generateKeyPair,
unlockPrivateKey,
encryptAndSign,
decryptAndVerify,
signDetached,
verifyDetached,
type KeyPair,
} from "../src/pgp.ts";
const PASSPHRASE = "correct horse battery staple";
let alice: KeyPair;
let alicePrivate: PrivateKey;
let bob: KeyPair;
before(async () => {
alice = await generateKeyPair(PASSPHRASE, "Alice", "[email protected]");
alicePrivate = await unlockPrivateKey(alice.privateKey, PASSPHRASE);
bob = await generateKeyPair(PASSPHRASE, "Bob", "[email protected]");
});
test("encrypts, signs, decrypts and verifies a message", async () => {
const plaintext = "attack at dawn";
const ciphertext = await encryptAndSign(plaintext, alice.publicKey, alicePrivate);
const result = await decryptAndVerify(ciphertext, alicePrivate, alice.publicKey);
assert.equal(result.text, plaintext);
assert.equal(result.verified, true);
});
test("detached signature verifies against the original message", async () => {
const plaintext = "detached payload";
const signature = await signDetached(plaintext, alicePrivate);
assert.equal(await verifyDetached(plaintext, signature, alice.publicKey), true);
});
test("detached signature does not verify with the wrong key", async () => {
const plaintext = "detached payload";
const signature = await signDetached(plaintext, alicePrivate);
let verified = false;
try {
verified = await verifyDetached(plaintext, signature, bob.publicKey);
} catch {
verified = false;
}
assert.equal(verified, false);
});
test("wrong private key cannot decrypt", async () => {
const ciphertext = await encryptAndSign("secret payload", alice.publicKey, alicePrivate);
const bobPrivate = await unlockPrivateKey(bob.privateKey, PASSPHRASE);
await assert.rejects(() => decryptAndVerify(ciphertext, bobPrivate, alice.publicKey));
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.