Skip to content

Latest commit

 

History

History
219 lines (166 loc) · 3.57 KB

EXAMPLES.md

File metadata and controls

219 lines (166 loc) · 3.57 KB

List Keys

import { gpg } from "@mykeels/gpg";

(async () => {
  const keys = await gpg.listKeys();
})();

Generate Key

import { gpg } from "@mykeels/gpg";

(async () => {
  const keys = await gpg.generateKey(
    "John Doe",
    "john.doe@mailinator.com",
    "sample-passphrase"
  );
})();

Export Public Key

import { gpg } from "@mykeels/gpg";

(async () => {
  const keys = await gpg.exportPublicKey("john.doe@mailinator.com");
})();

or

import { gpg } from "@mykeels/gpg";

(async () => {
  const keys = await gpg.exportPublicKey("John Doe <john.doe@mailinator.com>");
})();

Export Private Key

import { gpg } from "@mykeels/gpg";

(async () => {
  const keys = await gpg.listKeys();
  const keyGridId = keys.find(key => key.email === "john.doe@mailinator.com");
  const buffer = await gpg.exportPrivateKey(keyGridId); // this will be encrypted, if there's a passphrase
})();

Export Private Key as Base64

import { gpg } from "@mykeels/gpg";

(async () => {
  const keys = await gpg.listKeys();
  const keyGridId = keys.find(key => key.email === "john.doe@mailinator.com");
  const privateKey = await gpg.exportPrivateKeyAsBase64(keyGridId); // this will be encrypted, if there's a passphrase
})();

Import Key from File

import { gpg } from "@mykeels/gpg";

(async () => {
  const { fingerprint } = await gpg.importKeyFromFile(
    path.join(__dirname, "public.key")
  );
})();

Import Key from String

import { gpg } from "@mykeels/gpg";

(async () => {
  const { fingerprint } = await gpg.importKey(`THIS IS A SAMPLE PUBLIC KEY`);
})();

Remove Key

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.removeKey(`6F20F59D`); // pass the key's id
})();

Verify Signature

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.verifySignature("Hello, this is me!", [
    "--trust-model",
    "always",
    "--default-key",
    "6F20F59D",
  ]); // pass the key's id
})();

Encrypt String

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.encrypt("Hello World", [
    "recipient1@example.com",
    "recipient2@example.com",
  ]);
})();

Encrypt File

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.encryptFile("/path/to/file", [
    "recipient1@example.com",
    "recipient2@example.com",
  ]);
})();

Decrypt String

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.decrypt("SAMPLE-ENCRYPTED-STRING", "sample-passphrase");
})();

Decrypt File

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.decryptFile("/path/to/encrypted/file", "sample-passphrase");
})();

Other Operations

import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.call("<input>", [
    "--skip-verify",
    "--passphrase-fd",
    "0",
    "--decrypt",
    "./path/to/key.gpg",
  ]);
})();
import { gpg } from "@mykeels/gpg";

(async () => {
  await gpg.callStreaming(
    {
      source: "source",
      dest: "dest",
    },
    [
      "--decrypt",
      "--default-key",
      "6F20F59D",
      "--recipient",
      "6F20F59D",
      "--trust-model",
      "always",
    ]
  );
})();

Change GPG Base Directory

Unix uses ~/.gnupg Windows uses C:\Users\[User]\AppData\Roaming\gnupg

import { gpg, GPG_WINDOWS_BASE_DIR } from "@mykeels/gpg";

// gpg uses unix base directory by default

(async () => {
  gpg.setBaseDir(GPG_WINDOWS_BASE_DIR);
})();

Set Temp Directory for storing Encrypted Files for Decryption

gpg.setTempFolderPath("./temp");