doc: python bindings howto

* updated multi-encryption final example to be complete.
* second example shows most likely method of reading plaintext.
* updated example filenames to stick with running gag
  (i.e. secret_plans.txt).
This commit is contained in:
Ben McGinnes 2018-03-13 07:42:04 +11:00
parent a8f48b6f57
commit 484e9a6229

View File

@ -384,7 +384,7 @@
cipher = c.encrypt(text, recipients=rlogrus, sign=False, always_trust=True) cipher = c.encrypt(text, recipients=rlogrus, sign=False, always_trust=True)
afile = open("encrypted_file.txt.asc", "wb") afile = open("secret_plans.txt.asc", "wb")
afile.write(cipher[0]) afile.write(cipher[0])
afile.close() afile.close()
#+end_src #+end_src
@ -409,6 +409,20 @@
somewhat with something more like this: somewhat with something more like this:
#+begin_src python #+begin_src python
import gpg
afile = open("secret_plans.txt", "rb")
text = afile.read()
afile.close()
c = gpg.Context(armor=True)
rpattern = list(c.keylist(pattern="@gnupg.org", secret=False))
rlogrus = []
for i in range(len(rpattern)):
if rpattern[i].can_encrypt == 1:
rlogrus.append(rpattern[i])
try: try:
cipher = c.encrypt(text, recipients=rlogrus, add_encrypt_to=True) cipher = c.encrypt(text, recipients=rlogrus, add_encrypt_to=True)
except gpg.errors.InvalidRecipients as e: except gpg.errors.InvalidRecipients as e:
@ -422,6 +436,10 @@
cipher = c.encrypt(text, recipients=rlogrus, add_encrypt_to=True) cipher = c.encrypt(text, recipients=rlogrus, add_encrypt_to=True)
except: except:
pass pass
afile = open("secret_plans.txt.asc", "wb")
afile.write(cipher[0])
afile.close()
#+end_src #+end_src
This will attempt to encrypt to all the keys searched for, then This will attempt to encrypt to all the keys searched for, then