Merge pull request #896 from Hasimir/crypto-gpg-howto-update

doc: crypto.rst — GPGME HOWTO update
This commit is contained in:
2018-04-27 04:11:45 -04:00
committed by GitHub
+19 -16
View File
@@ -45,6 +45,8 @@ 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. 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.
A more comprehensive `GPGME Python Bindings HOWTO <https://dev.gnupg.org/source/gpgme/browse/master/lang/python/docs/GPGMEpythonHOWTOen.org>`_ is available with the source and a HTML version is available `here <http://files.au.adversary.org/crypto/GPGMEpythonHOWTOen.html>`_. Python 3 sample scripts from the examples in the HOWTO are also provided with the source and are accessible `here <https://dev.gnupg.org/source/gpgme/browse/master/lang/python/examples/howto/>`_.
Available under the same terms as the rest of the GnuPG Project: GPLv2 and LGPLv2.1, both with the "or any later version" clause. 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 Installation
@@ -58,27 +60,28 @@ Example
.. code-block:: python3 .. code-block:: python3
import gpg import gpg
import os
# Encryption to public key specified in rkey. # Encryption to public key specified in rkey.
rkey = "0xDEADBEEF" a_key = input("Enter the fingerprint or key ID to encrypt to: ")
text = "Something to hide." filename = input("Enter the filename to encrypt: ")
plain = gpg.core.Data(text) with open(filename, "rb") as afile:
cipher = gpg.core.Data() text = afile.read()
c = gpg.core.Context() c = gpg.core.Context(armor=True)
c.set_armor(1) rkey = list(c.keylist(pattern=a_key, secret=False))
c.op_keylist_start(rkey, 0) ciphertext, result, sign_result = c.encrypt(text, recipients=rkey,
r = c.op_keylist_next() always_trust=True,
c.op_encrypt([r], 1, plain, cipher) add_encrypt_to=True)
cipher.seek(0, os.SEEK_SET) with open("{0}.asc".format(filename), "wb") as bfile:
ciphertext = cipher.read() bfile.write(ciphertext)
# Decryption with corresponding secret key # Decryption with corresponding secret key
# invokes gpg-agent and pinentry. # invokes gpg-agent and pinentry.
plaintext = gpg.Context().decrypt(ciphertext) with open("{0}.asc".format(filename), "rb") as cfile:
plaintext, result, verify_result = gpg.Context().decrypt(cfile)
with open("new-{0}".format(filename), "wb") as dfile:
dfile.write(plaintext)
# Matching the data. # Matching the data.
if text == plaintext[0].decode("utf-8"): # Also running a diff on filename and the new filename should match.
if text == plaintext:
print("Hang on ... did you say *all* of GnuPG? Yep.") print("Hang on ... did you say *all* of GnuPG? Yep.")
else: else:
pass pass