Skip to content

python-gnupg

python-gnupg is a Python library to interact with gpg taking care of the internal details and allows its users to generate and manage keys, encrypt and decrypt data, and sign and verify messages.

Installation

pip install python-gnupg

Usage

You interface to the GnuPG functionality through an instance of the GPG class:

gpg = gnupg.GPG(gnupghome="/path/to/home/directory")

Note: I've already created an adapter for gpg called KeyStore available in pass-collaborate

  • Decrypt a file:
gpg.decrypt_file("path/to/file")

Note: You can't pass Path arguments to decrypt_file.

gpg.encrypt_file('path/to/file', recipients)

Where recipients is a List[str] of gpg Key IDs.

>>> public_keys = gpg.list_keys()
>>> private_keys = gpg.list_keys(True)
def list_recipients(self, path: Path) -> List['GPGKey']:
    """List the keys that can decrypt a file.

    Args:
        path: Path to the file to check.
    """
    keys = []
    for short_key in self.gpg.get_recipients_file(str(path)):
        keys.append(self.gpg.list_keys(keys=[short_key])[0]['fingerprint'])

    return keys
import_result = gpg.recv_keys('server-name', 'keyid1', 'keyid2', ...)

References