Skip to content

Generate a PGP Key Pair

ts
// Generate a 2048-bit RSA key pair and save to files
var err = GeneratePGPKeys("/path/to/keys", "alice", 2048);
// Creates: /path/to/keys/alice.pubkey and /path/to/keys/alice.privkey

Generates a new RSA PGP key pair and saves the armored public and private keys as files in the specified directory.

Parameters

NameTypeDescription
dirstringDirectory where the key files will be saved
keyNamestringBase name for the key files (no extension)
bitsnumberRSA key size (recommended: 2048 or 4096)
  • The public key will be saved as <keyName>.pubkey
  • The private key will be saved as <keyName>.privkey

Returns

  • Returns null on success, or an error object if key generation fails.

NOTE

Always protect your private key files. Use secure file permissions and consider encrypting them with a passphrase.

Example

ts
var err = GeneratePGPKeys("/keys", "alice", 2048);
if (err == null) {
  Log("Key pair generated successfully!");
} else {
  Log("Key generation failed: " + err);
}