mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
GPGME Python bindings
* Added GPGME bindings description and the standard encryption and decryption example.
This commit is contained in:
@@ -40,6 +40,52 @@ Example code using high level symmetric encryption recipe:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
GPGME bindings
|
||||||
|
--------------
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Available under the same terms as the rest of the GnuPG Project: GPLv2 and LGPLv2.1, both with the "or any later version" clause.
|
||||||
|
|
||||||
|
Installation
|
||||||
|
------------
|
||||||
|
|
||||||
|
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).
|
||||||
|
|
||||||
|
Example
|
||||||
|
-------
|
||||||
|
|
||||||
|
.. code-block:: python3
|
||||||
|
|
||||||
|
import gpg
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Encryption to public key specified in rkey.
|
||||||
|
rkey = "0xDEADBEEF"
|
||||||
|
text = "Something to hide."
|
||||||
|
plain = gpg.core.Data(text)
|
||||||
|
cipher = gpg.core.Data()
|
||||||
|
c = gpg.core.Context()
|
||||||
|
c.set_armor(1)
|
||||||
|
c.op_keylist_start(rkey, 0)
|
||||||
|
r = c.op_keylist_next()
|
||||||
|
c.op_encrypt([r], 1, plain, cipher)
|
||||||
|
cipher.seek(0, os.SEEK_SET)
|
||||||
|
ciphertext = cipher.read()
|
||||||
|
|
||||||
|
# Decryption with corresponding secret key
|
||||||
|
# invokes gpg-agent and pinentry.
|
||||||
|
plaintext = gpg.Context().decrypt(ciphertext)
|
||||||
|
|
||||||
|
# Matching the data.
|
||||||
|
if text == plaintext[0].decode("utf-8"):
|
||||||
|
print("Hang on ... did you say *all* of GnuPG? Yep.")
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PyCrypto
|
PyCrypto
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user