doc: python bindings howto
* Promoted final encryption example so that it will appear as heading 6.1.3 when exported to HTML or PDF.
This commit is contained in:
parent
3d0c7a2202
commit
94a95ac123
@ -608,71 +608,71 @@
|
|||||||
This will attempt to encrypt to all the keys searched for, then
|
This will attempt to encrypt to all the keys searched for, then
|
||||||
remove invalid recipients if it fails and try again.
|
remove invalid recipients if it fails and try again.
|
||||||
|
|
||||||
**** Encrypting to one key using the second method
|
*** Encrypting to one key using the second method
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:CUSTOM_ID: howto-basic-encryption-monogamous
|
:CUSTOM_ID: howto-basic-encryption-monogamous
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
This example re-creates the first encryption example except it
|
This example re-creates the first encryption example except it
|
||||||
uses the same =encrypt= method used in the subsequent examples
|
uses the same =encrypt= method used in the subsequent examples
|
||||||
instead of the =op_encrypt= method. This means that, unlike the
|
instead of the =op_encrypt= method. This means that, unlike the
|
||||||
=op_encrypt= method, it /must/ use byte literal input data.
|
=op_encrypt= method, it /must/ use byte literal input data.
|
||||||
|
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
import gpg
|
import gpg
|
||||||
|
|
||||||
rkey = "0x12345678DEADBEEF"
|
rkey = "0x12345678DEADBEEF"
|
||||||
text = b"""Some text to test with.
|
text = b"""Some text to test with.
|
||||||
|
|
||||||
Since the text in this case must be bytes, it is most likely that
|
Since the text in this case must be bytes, it is most likely that
|
||||||
the input form will be a separate file which is opened with "rb"
|
the input form will be a separate file which is opened with "rb"
|
||||||
as this is the simplest method of obtaining the correct data
|
as this is the simplest method of obtaining the correct data
|
||||||
format.
|
format.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
c = gpg.Context(armor=True)
|
c = gpg.Context(armor=True)
|
||||||
rpattern = list(c.keylist(pattern=rkey, secret=False))
|
rpattern = list(c.keylist(pattern=rkey, secret=False))
|
||||||
logrus = []
|
logrus = []
|
||||||
|
|
||||||
for i in range(len(rpattern)):
|
for i in range(len(rpattern)):
|
||||||
if rpattern[i].can_encrypt == 1:
|
if rpattern[i].can_encrypt == 1:
|
||||||
logrus.append(rpattern[i])
|
logrus.append(rpattern[i])
|
||||||
|
|
||||||
cipher = c.encrypt(text, recipients=logrus, sign=False, always_trust=True)
|
cipher = c.encrypt(text, recipients=logrus, sign=False, always_trust=True)
|
||||||
|
|
||||||
afile = open("secret_plans.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
|
||||||
|
|
||||||
With one or two exceptions, this method will probably prove to be
|
With one or two exceptions, this method will probably prove to be
|
||||||
easier to implement than the first method and thus it is the
|
easier to implement than the first method and thus it is the
|
||||||
recommended encryption method. Though it is even more likely to
|
recommended encryption method. Though it is even more likely to
|
||||||
be used like this:
|
be used like this:
|
||||||
|
|
||||||
#+begin_src python
|
#+begin_src python
|
||||||
import gpg
|
import gpg
|
||||||
|
|
||||||
rkey = "0x12345678DEADBEEF"
|
rkey = "0x12345678DEADBEEF"
|
||||||
|
|
||||||
afile = open("secret_plans.txt", "rb")
|
afile = open("secret_plans.txt", "rb")
|
||||||
text = afile.read()
|
text = afile.read()
|
||||||
afile.close()
|
afile.close()
|
||||||
|
|
||||||
c = gpg.Context(armor=True)
|
c = gpg.Context(armor=True)
|
||||||
rpattern = list(c.keylist(pattern=rkey, secret=False))
|
rpattern = list(c.keylist(pattern=rkey, secret=False))
|
||||||
logrus = []
|
logrus = []
|
||||||
|
|
||||||
for i in range(len(rpattern)):
|
for i in range(len(rpattern)):
|
||||||
if rpattern[i].can_encrypt == 1:
|
if rpattern[i].can_encrypt == 1:
|
||||||
logrus.append(rpattern[i])
|
logrus.append(rpattern[i])
|
||||||
|
|
||||||
cipher = c.encrypt(text, recipients=logrus, sign=False, always_trust=True)
|
cipher = c.encrypt(text, recipients=logrus, sign=False, always_trust=True)
|
||||||
|
|
||||||
afile = open("secret_plans.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
|
||||||
|
|
||||||
|
|
||||||
** Decryption
|
** Decryption
|
||||||
|
Loading…
Reference in New Issue
Block a user