The gnupg module enables Python to use the functionality of the GNU Privacy Guard or GnuPG. With this module, Python programs can create and manage keys, encrypt and decrypt data, and sign and verify documents. (What is the difference between PGP, OpenPGP, and GnuPG ?)
Installing The gnupg Module
Install the module using ‘pip install python-gnupg’.
Getting Started:
You would need to import the module to be able to use it in Python.
>>> import gnupg >>> gpg = gnupg.GPG(gnupghome='/path/to/home/directory')
In GnuPG, the “home directory” is used for public and private keyring files and trust database. If no parameter is passed, the default directory is picked up in GnuPG.
GPG constructor also accepts a few additional parameters.
- gpgbinary: the path to gpg executable
- verbose
- user_agent: uses in-memory GPG agent if specified True.
- keyring: specified value is used as the name of the keyring file.
- options: additional command-line options passed to GPG
- secret_keyring: specified value is used as the name of the secret keyring file
Generating GPG Keys:
You can generate GPG keys in Python as follows:
>>> key = gpg.gen_key(input_data)
iput_data specifies the parameters of GnuPG. By default, it creates an RSA key of 1024 bits. The real name is taken as “Autogenerated Key” and email-id as <username>@hostname.
You can generate the string input_data using the following method:
>>> input_data = gpg.gen_key_input(key_type="RSA", key_length=1024)
gen_key_input() takes the following parameter:
0 Comments