Skip to content

Instantly share code, notes, and snippets.

@sai-krishna-acceleron
Created October 1, 2020 03:34
Show Gist options
  • Save sai-krishna-acceleron/8adcc621b264097a4ef49c44550703b8 to your computer and use it in GitHub Desktop.
Save sai-krishna-acceleron/8adcc621b264097a4ef49c44550703b8 to your computer and use it in GitHub Desktop.
Generating keys with GPG
import os
import gnupg
pwd = os.getcwd()
gpg = gnupg.GPG(gnupghome=pwd, verbose=False)
gpg.encoding = "utf-8"
key_input = gpg.gen_key_input(key_type="RSA", key_length=2048, name_real="Si Ki", name_comment="dev",
name_email="io@acceleron")
key = gpg.gen_key(key_input)
ascii_armored_public_keys = gpg.export_keys(str(key), expect_passphrase=False)
ascii_armored_private_keys = gpg.export_keys(str(key), True, expect_passphrase=False)
with open('keyfile.asc', 'w') as f:
f.write(ascii_armored_public_keys)
f.write(ascii_armored_private_keys)
@sai-krishna-acceleron
Copy link
Author

Requirements:

  • install gnupg on mac (process might be different for linux and windows) brew install gnupg2

  • add this to rc file export GPG_TTY=$(tty)

  • (Optional) brew install pinentry-mac for reading passphrases through terminal. Link

Prerequisites:

  • GPG needs a home directory to work out of (gnupghome argument), where it stores all the info necessary, for example its keystore (which is called a keyring), it's trust database.

  • Please refer to this for issues relating to randomness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment