Skip to content
This repository has been archived by the owner on Jul 29, 2018. It is now read-only.

GPG in Python

Mike Tigas edited this page Apr 29, 2014 · 3 revisions

A scratchpad of testing and fiddling around with python-gnupg.

python-gnupg doesn't support symlinked gnupg binaries (see isislovecruft/python-gnupg#32) so when using homebrew to set up (as per current bootstrap doc for Mac), we need to use absolute path to the cellar'd binary.

>>> import gnupg
>>> gpg = gnupg.GPG()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mtigas/.virtualenvs/odi-authentication/lib/python2.7/site-packages/gnupg/gnupg.py", line 127, in __init__
    use_agent=use_agent,)
  File "/Users/mtigas/.virtualenvs/odi-authentication/lib/python2.7/site-packages/gnupg/_meta.py", line 169, in __init__
    raise RuntimeError(ae.message)
RuntimeError: Could not find binary gpg2
>>> gpg = gnupg.GPG("/usr/local/Cellar/gnupg2/2.0.20/bin/gpg2")
>>>

but now we have a gpg object that we can toy around with. like generating new pgp keys! and signing files!

import gnupg

gpg = gnupg.GPG("/usr/local/Cellar/gnupg2/2.0.20/bin/gpg2",homedir="secret_data")
key_identity = {
  'name_real': 'Authentication Test',
  'name_email': '[email protected]',
  'expire_date': '2015-01-01',
  'key_type': 'RSA',
  'key_length': 4096,
  'key_usage': '',
  'subkey_type': 'RSA',
  'subkey_length': 4096,
  'subkey_usage': 'encrypt,sign,auth',
  'passphrase': 'sekrit'
}
key_input = gpg.gen_key_input(**key_identity)
key = gpg.gen_key(key_input)

with open('README.md', 'rb') as f:
  s = gpg.sign(data=f, default_key=key, passphrase="sekrit", clearsign=True, detach=True, binary=False)

print s.data
Clone this wiki locally