Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa682c5

Browse files
authored
Merge pull request #896 from Hasimir/crypto-gpg-howto-update
doc: crypto.rst — GPGME HOWTO update
2 parents 5613daf + 50e04af commit aa682c5

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

docs/scenarios/crypto.rst

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ GPGME bindings
4545

4646
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.
4747

48+
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/>`_.
49+
4850
Available under the same terms as the rest of the GnuPG Project: GPLv2 and LGPLv2.1, both with the "or any later version" clause.
4951

5052
Installation
@@ -58,27 +60,28 @@ Example
5860
.. code-block:: python3
5961
6062
import gpg
61-
import os
6263
6364
# 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-
65+
a_key = input("Enter the fingerprint or key ID to encrypt to: ")
66+
filename = input("Enter the filename to encrypt: ")
67+
with open(filename, "rb") as afile:
68+
text = afile.read()
69+
c = gpg.core.Context(armor=True)
70+
rkey = list(c.keylist(pattern=a_key, secret=False))
71+
ciphertext, result, sign_result = c.encrypt(text, recipients=rkey,
72+
always_trust=True,
73+
add_encrypt_to=True)
74+
with open("{0}.asc".format(filename), "wb") as bfile:
75+
bfile.write(ciphertext)
7676
# Decryption with corresponding secret key
7777
# invokes gpg-agent and pinentry.
78-
plaintext = gpg.Context().decrypt(ciphertext)
79-
78+
with open("{0}.asc".format(filename), "rb") as cfile:
79+
plaintext, result, verify_result = gpg.Context().decrypt(cfile)
80+
with open("new-{0}".format(filename), "wb") as dfile:
81+
dfile.write(plaintext)
8082
# Matching the data.
81-
if text == plaintext[0].decode("utf-8"):
83+
# Also running a diff on filename and the new filename should match.
84+
if text == plaintext:
8285
print("Hang on ... did you say *all* of GnuPG? Yep.")
8386
else:
8487
pass

0 commit comments

Comments
 (0)