docs: python bindings howto

* Another attempt at fixing the org-mode version.
* A proof reader ascertained there were tabs in it instead of whitespace.
* Stripped the lot out and replaced with standard 4 spaces, fixed
  every incorrect example ... and it still breaks upon save and/or export.
* Added the reference to the mutt-groups.py script to demonstrate the
  groups.py module/code.
This commit is contained in:
Ben McGinnes 2018-06-03 00:46:46 +10:00
parent e144a6d706
commit 897423422b

View File

@ -517,7 +517,7 @@
ciphertext, result, sign_result = c.encrypt(text, recipients=rkey, sign=False) ciphertext, result, sign_result = c.encrypt(text, recipients=rkey, sign=False)
with open("secret_plans.txt.asc", "wb") as afile: with open("secret_plans.txt.asc", "wb") as afile:
afile.write(ciphertext) afile.write(ciphertext)
#+end_src #+end_src
Though this is even more likely to be used like this; with the Though this is even more likely to be used like this; with the
@ -532,12 +532,13 @@
a_key = "0x12345678DEADBEEF" a_key = "0x12345678DEADBEEF"
with open("secret_plans.txt", "rb") as afile: with open("secret_plans.txt", "rb") as afile:
text = afile.read() text = afile.read()
c = gpg.Context(armor=True) c = gpg.Context(armor=True)
rkey = list(c.keylist(pattern=a_key, secret=False)) rkey = list(c.keylist(pattern=a_key, secret=False))
ciphertext, result, sign_result = c.encrypt(text, recipients=rkey, ciphertext, result, sign_result = c.encrypt(text, recipients=rkey,
sign=True, always_trust=True, add_encrypt_to=True) sign=True, always_trust=True,
add_encrypt_to=True)
with open("secret_plans.txt.asc", "wb") as afile: with open("secret_plans.txt.asc", "wb") as afile:
afile.write(ciphertext) afile.write(ciphertext)
@ -587,8 +588,8 @@
if rpattern[i].can_encrypt == 1: if rpattern[i].can_encrypt == 1:
logrus.append(rpattern[i]) logrus.append(rpattern[i])
ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, sign=False, ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
always_trust=True) sign=False, always_trust=True)
with open("secret_plans.txt.asc", "wb") as afile: with open("secret_plans.txt.asc", "wb") as afile:
afile.write(ciphertext) afile.write(ciphertext)
@ -600,8 +601,8 @@
#+begin_src python #+begin_src python
ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
always_trust=True, always_trust=True,
add_encrypt_to=True) add_encrypt_to=True)
#+end_src #+end_src
The only keyword arguments requiring modification are those for The only keyword arguments requiring modification are those for
@ -618,7 +619,7 @@
import gpg import gpg
with open("secret_plans.txt.asc", "rb") as afile: with open("secret_plans.txt.asc", "rb") as afile:
text = afile.read() text = afile.read()
c = gpg.Context(armor=True) c = gpg.Context(armor=True)
rpattern = list(c.keylist(pattern="@gnupg.org", secret=False)) rpattern = list(c.keylist(pattern="@gnupg.org", secret=False))
@ -628,24 +629,24 @@
if rpattern[i].can_encrypt == 1: if rpattern[i].can_encrypt == 1:
logrus.append(rpattern[i]) logrus.append(rpattern[i])
try:
ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
add_encrypt_to=True)
except gpg.errors.InvalidRecipients as e:
for i in range(len(e.recipients)):
for n in range(len(logrus)):
if logrus[n].fpr == e.recipients[i].fpr:
logrus.remove(logrus[n])
else:
pass
try: try:
ciphertext, result, sign_result = c.encrypt(text, recipients=logrus, ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
add_encrypt_to=True) add_encrypt_to=True)
except: except gpg.errors.InvalidRecipients as e:
pass for i in range(len(e.recipients)):
for n in range(len(logrus)):
if logrus[n].fpr == e.recipients[i].fpr:
logrus.remove(logrus[n])
else:
pass
with open("secret_plans.txt.asc", "wb") as afile: try:
afile.write(ciphertext) ciphertext, result, sign_result = c.encrypt(text, recipients=logrus,
add_encrypt_to=True)
with open("secret_plans.txt.asc", "wb") as afile:
afile.write(ciphertext)
except:
pass
#+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
@ -776,13 +777,13 @@
import gpg import gpg
with open("/path/to/statement.txt", "rb") as tfile: with open("/path/to/statement.txt", "rb") as tfile:
text = tfile.read() text = tfile.read()
c = gpg.Context() c = gpg.Context()
signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.NORMAL) signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.NORMAL)
with open("/path/to/statement.txt.sig", "wb") as afile: with open("/path/to/statement.txt.sig", "wb") as afile:
afile.write(signed_data) afile.write(signed_data)
#+end_src #+end_src
@ -808,7 +809,7 @@
signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.DETACH) signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.DETACH)
with open("/path/to/statement.txt.asc", "w") as afile: with open("/path/to/statement.txt.asc", "w") as afile:
afile.write(signed_data.decode()) afile.write(signed_data.decode())
#+end_src #+end_src
As with normal signatures, detached signatures are best handled as As with normal signatures, detached signatures are best handled as
@ -818,13 +819,13 @@
import gpg import gpg
with open("/path/to/statement.txt", "rb") as tfile: with open("/path/to/statement.txt", "rb") as tfile:
text = tfile.read() text = tfile.read()
c = gpg.Context(signers=sig_src) c = gpg.Context(signers=sig_src)
signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.DETACH) signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.DETACH)
with open("/path/to/statement.txt.sig", "wb") as afile: with open("/path/to/statement.txt.sig", "wb") as afile:
afile.write(signed_data) afile.write(signed_data)
#+end_src #+end_src
@ -860,13 +861,13 @@
import gpg import gpg
with open("/path/to/statement.txt", "rb") as tfile: with open("/path/to/statement.txt", "rb") as tfile:
text = tfile.read() text = tfile.read()
c = gpg.Context() c = gpg.Context()
signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.CLEAR) signed_data, result = c.sign(text, mode=gpg.constants.sig.mode.CLEAR)
with open("/path/to/statement.txt.asc", "wb") as afile: with open("/path/to/statement.txt.asc", "wb") as afile:
afile.write(signed_data) afile.write(signed_data)
#+end_src #+end_src
@ -906,8 +907,8 @@
{0} {0}
with key {1} with key {1}
made at {2} made at {2}
""".format(c.get_key(sign.fpr).uids[0].uid, """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
sign.fpr, time.ctime(sign.timestamp))) time.ctime(sign.timestamp)))
else: else:
pass pass
#+end_src #+end_src
@ -938,8 +939,8 @@
{0} {0}
with key {1} with key {1}
made at {2} made at {2}
""".format(c.get_key(sign.fpr).uids[0].uid, """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
sign.fpr, time.ctime(sign.timestamp))) time.ctime(sign.timestamp)))
else: else:
pass pass
#+end_src #+end_src
@ -987,8 +988,8 @@
{0} {0}
with key {1} with key {1}
made at {2} made at {2}
""".format(c.get_key(sign.fpr).uids[0].uid, """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
sign.fpr, time.ctime(sign.timestamp))) time.ctime(sign.timestamp)))
else: else:
pass pass
#+end_src #+end_src
@ -1009,15 +1010,15 @@
verified = False verified = False
print(e) print(e)
if verified is not None: if verified is True:
for i in range(len(result.signatures)): for i in range(len(result.signatures)):
sign = result.signatures[i] sign = result.signatures[i]
print("""Good signature from: print("""Good signature from:
{0} {0}
with key {1} with key {1}
made at {2} made at {2}
""".format(c.get_key(sign.fpr).uids[0].uid, """.format(c.get_key(sign.fpr).uids[0].uid, sign.fpr,
sign.fpr, time.ctime(sign.timestamp))) time.ctime(sign.timestamp)))
else: else:
pass pass
#+end_src #+end_src
@ -1109,15 +1110,15 @@
returned =GenkeyResult= object, which includes the following data: returned =GenkeyResult= object, which includes the following data:
#+begin_src python #+begin_src python
print(""" print("""
Fingerprint: {0} Fingerprint: {0}
Primary Key: {1} Primary Key: {1}
Public Key: {2} Public Key: {2}
Secret Key: {3} Secret Key: {3}
Sub Key: {4} Sub Key: {4}
User IDs: {5} User IDs: {5}
""".format(dmkey.fpr, dmkey.primary, dmkey.pubkey, dmkey.seckey, dmkey.sub, """.format(dmkey.fpr, dmkey.primary, dmkey.pubkey, dmkey.seckey, dmkey.sub,
dmkey.uid)) dmkey.uid))
#+end_src #+end_src
Alternatively the information can be confirmed using the command Alternatively the information can be confirmed using the command
@ -1128,7 +1129,7 @@
~/.gnupg-dm/pubring.kbx ~/.gnupg-dm/pubring.kbx
---------------------- ----------------------
sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15] sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15]
177B7C25DB99745EE2EE13ED026D2F19E99E63AA 177B7C25DB99745EE2EE13ED026D2F19E99E63AA
uid [ultimate] Danger Mouse <dm@secret.example.net> uid [ultimate] Danger Mouse <dm@secret.example.net>
bash-4.4$ bash-4.4$
@ -1145,15 +1146,15 @@
Secret key is available. Secret key is available.
sec rsa3072/026D2F19E99E63AA sec rsa3072/026D2F19E99E63AA
created: 2018-03-15 expires: 2019-03-15 usage: SC created: 2018-03-15 expires: 2019-03-15 usage: SC
trust: ultimate validity: ultimate trust: ultimate validity: ultimate
[ultimate] (1). Danger Mouse <dm@secret.example.net> [ultimate] (1). Danger Mouse <dm@secret.example.net>
[ultimate] (1). Danger Mouse <dm@secret.example.net> [ultimate] (1). Danger Mouse <dm@secret.example.net>
Cipher: TWOFISH, CAMELLIA256, AES256, CAMELLIA192, AES192, CAMELLIA128, AES, BLOWFISH, IDEA, CAST5, 3DES Cipher: TWOFISH, CAMELLIA256, AES256, CAMELLIA192, AES192, CAMELLIA128, AES, BLOWFISH, IDEA, CAST5, 3DES
Digest: SHA512, SHA384, SHA256, SHA224, RIPEMD160, SHA1 Digest: SHA512, SHA384, SHA256, SHA224, RIPEMD160, SHA1
Compression: ZLIB, BZIP2, ZIP, Uncompressed Compression: ZLIB, BZIP2, ZIP, Uncompressed
Features: MDC, Keyserver no-modify Features: MDC, Keyserver no-modify
bash-4.4$ bash-4.4$
#+end_src #+end_src
@ -1189,15 +1190,15 @@
As with the primary key, the results here can be checked with: As with the primary key, the results here can be checked with:
#+begin_src python #+begin_src python
print(""" print("""
Fingerprint: {0} Fingerprint: {0}
Primary Key: {1} Primary Key: {1}
Public Key: {2} Public Key: {2}
Secret Key: {3} Secret Key: {3}
Sub Key: {4} Sub Key: {4}
User IDs: {5} User IDs: {5}
""".format(dmsub.fpr, dmsub.primary, dmsub.pubkey, dmsub.seckey, dmsub.sub, """.format(dmsub.fpr, dmsub.primary, dmsub.pubkey, dmsub.seckey, dmsub.sub,
dmsub.uid)) dmsub.uid))
#+end_src #+end_src
As well as on the command line with: As well as on the command line with:
@ -1207,7 +1208,7 @@
~/.gnupg-dm/pubring.kbx ~/.gnupg-dm/pubring.kbx
---------------------- ----------------------
sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15] sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15]
177B7C25DB99745EE2EE13ED026D2F19E99E63AA 177B7C25DB99745EE2EE13ED026D2F19E99E63AA
uid [ultimate] Danger Mouse <dm@secret.example.net> uid [ultimate] Danger Mouse <dm@secret.example.net>
ssb rsa3072 2018-03-15 [E] [expires: 2018-09-13] ssb rsa3072 2018-03-15 [E] [expires: 2018-09-13]
@ -1251,7 +1252,7 @@
~/.gnupg-dm/pubring.kbx ~/.gnupg-dm/pubring.kbx
---------------------- ----------------------
sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15] sec rsa3072 2018-03-15 [SC] [expires: 2019-03-15]
177B7C25DB99745EE2EE13ED026D2F19E99E63AA 177B7C25DB99745EE2EE13ED026D2F19E99E63AA
uid [ultimate] Danger Mouse <danger.mouse@secret.example.net> uid [ultimate] Danger Mouse <danger.mouse@secret.example.net>
uid [ultimate] Danger Mouse <dm@secret.example.net> uid [ultimate] Danger Mouse <dm@secret.example.net>
ssb rsa3072 2018-03-15 [E] [expires: 2018-09-13] ssb rsa3072 2018-03-15 [E] [expires: 2018-09-13]
@ -1348,7 +1349,7 @@
if lines[i].startswith("group") is True: if lines[i].startswith("group") is True:
line = lines[i] line = lines[i]
else: else:
pass pass
groups = line.split(":")[-1].replace('"', '').split(',') groups = line.split(":")[-1].replace('"', '').split(',')
@ -1372,6 +1373,12 @@
=group_lines[i][0]= as the name of the group, but =group_lines[i][0]= as the name of the group, but
=group_lists[i][1]= is the key IDs of the group as a string. =group_lists[i][1]= is the key IDs of the group as a string.
A demonstration of using the =groups.py= module is also available
in the form of the executable =mutt-groups.py= script. This second
script reads all the group entries in a user's =gpg.conf= file and
converts them into crypt-hooks suitable for use with the Mutt and
Neomutt mail clients.
* Copyright and Licensing * Copyright and Licensing
:PROPERTIES: :PROPERTIES: