Skip to content

Commit c88ed62

Browse files
authored
Merge pull request realpython#886 from Hasimir/crypto-updates
GPGME Python bindings
2 parents f7bbb1a + 35450c3 commit c88ed62

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

docs/scenarios/crypto.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,52 @@ Example code using high level symmetric encryption recipe:
4040
4141
4242
43+
GPGME bindings
44+
--------------
45+
46+
The `GPGME Python bindings <https://dev.gnupg.org/source/gpgme/browse/master/lang/python/>`_ provide pythonic access to `GPG Made Easy <https://dev.gnupg.org/source/gpgme/browse/master/>`_, a C API for the entire GNU Privacy Guard suite of projects, including GPG, libgcrypt and gpgsm (the S/MIME engine). It supports Python 2.6, 2.7, 3.4 and above. Depends on the SWIG C interface for Python as well as the GnuPG software and libraries.
47+
48+
Available under the same terms as the rest of the GnuPG Project: GPLv2 and LGPLv2.1, both with the "or any later version" clause.
49+
50+
Installation
51+
------------
52+
53+
Included by default when compiling GPGME if the configure script locates a supported python version (which it will if it's in $PATH during configuration).
54+
55+
Example
56+
-------
57+
58+
.. code-block:: python3
59+
60+
import gpg
61+
import os
62+
63+
# Encryption to public key specified in rkey.
64+
rkey = "0xDEADBEEF"
65+
text = "Something to hide."
66+
plain = gpg.core.Data(text)
67+
cipher = gpg.core.Data()
68+
c = gpg.core.Context()
69+
c.set_armor(1)
70+
c.op_keylist_start(rkey, 0)
71+
r = c.op_keylist_next()
72+
c.op_encrypt([r], 1, plain, cipher)
73+
cipher.seek(0, os.SEEK_SET)
74+
ciphertext = cipher.read()
75+
76+
# Decryption with corresponding secret key
77+
# invokes gpg-agent and pinentry.
78+
plaintext = gpg.Context().decrypt(ciphertext)
79+
80+
# Matching the data.
81+
if text == plaintext[0].decode("utf-8"):
82+
print("Hang on ... did you say *all* of GnuPG? Yep.")
83+
else:
84+
pass
85+
86+
87+
88+
4389
PyCrypto
4490
--------
4591

0 commit comments

Comments
 (0)