Skip to content

Generate a PGP Key Pair

ts
function GeneratePGPKeys(keyPairName: string, directory: string, bits: number): boolean;

Generates a new RSA PGP key pair and saves the armored public and private keys as separate files in directory. The key files are named after keyPairName with .pubkey and .privkey extensions.

Parameters

NameTypeDescription
keyPairNamestringBase name for the key files (no extension)
directorystringDirectory where the key files will be saved
bitsnumberRSA key size in bits (recommended: 2048 or 4096)
  • The public key will be saved as <directory>/<keyPairName>.pubkey
  • The private key will be saved as <directory>/<keyPairName>.privkey

Returns

Return valueExplanation
trueKey pair generated successfully
falseGeneration failed (invalid path, insufficient permissions, etc.)

NOTE

Always protect your private key files. Use secure file permissions and consider storing the key path in the secret store rather than hardcoding it in your script.

Example

ts
// Generate a 4096-bit RSA key pair for alice
var ok = GeneratePGPKeys("alice", "/keys", 4096);
if (ok) {
  Log("Key pair generated: /keys/alice.pubkey and /keys/alice.privkey");
} else {
  Log("Key generation failed.");
}