aboutsummaryrefslogtreecommitdiffstats
path: root/lang/python/doc/src/gpgme-python-howto
diff options
context:
space:
mode:
authorBen McGinnes <[email protected]>2018-12-10 20:14:28 +0000
committerBen McGinnes <[email protected]>2018-12-10 20:14:28 +0000
commit2e7a14c9b369096775a035091c197f2d438142a0 (patch)
tree651aaef023f839f92597fac266cd9bdd4415c8ab /lang/python/doc/src/gpgme-python-howto
parentpython: key import via HKP example (diff)
downloadgpgme-2e7a14c9b369096775a035091c197f2d438142a0.tar.gz
gpgme-2e7a14c9b369096775a035091c197f2d438142a0.zip
python: HKP search and import updates
* Tweaked the code again so that it can also handle the cases where someone has included a hexadecimal string in their user ID. * Updated the HOWTO to match. * Exported to .rst and .texi.
Diffstat (limited to 'lang/python/doc/src/gpgme-python-howto')
-rw-r--r--lang/python/doc/src/gpgme-python-howto67
1 files changed, 55 insertions, 12 deletions
diff --git a/lang/python/doc/src/gpgme-python-howto b/lang/python/doc/src/gpgme-python-howto
index 9820d86b..0c1239b9 100644
--- a/lang/python/doc/src/gpgme-python-howto
+++ b/lang/python/doc/src/gpgme-python-howto
@@ -963,6 +963,14 @@ relative ease by which such key IDs can be reproduced, as demonstrated
by the Evil32 Project in 2014 (which was subsequently exploited in
2016).
+Testing for whether a string in any given search is or may be a
+hexadecimal value which may be missing the leading =0x= is a simple
+matter of using a try/except statement which attempts to convert the
+string as hex to an integer and then back to hex; then using that to
+search with. Raising a ValueError simply results in treating the
+string as a string. This is the method and logic utilised in the
+=import-keys-hkp.py= script (see below).
+
*** Working with ProtonMail
:PROPERTIES:
@@ -1097,6 +1105,7 @@ import sys
c = gpg.Context()
server = hkp4py.KeyServer("hkps://hkps.pool.sks-keyservers.net")
results = []
+keys = []
if len(sys.argv) > 2:
pattern = " ".join(sys.argv[1:])
@@ -1105,22 +1114,56 @@ elif len(sys.argv) == 2:
else:
pattern = input("Enter the pattern to search for keys or user IDs: ")
-try:
- keys = server.search(pattern)
- print("Found {0} key(s).".format(len(keys)))
-except Exception as e:
- keys = []
+
+if pattern is not None:
+ try:
+ key = server.search(hex(int(pattern, 16)))
+ keyed = True
+ except ValueError as ve:
+ key = server.search(pattern)
+ keyed = False
+
+ if key is not None:
+ keys.append(key[0])
+ if keyed is True:
+ try:
+ fob = server.search(pattern)
+ except:
+ fob = None
+ if fob is not None:
+ keys.append(fob[0])
+ else:
+ pass
+ else:
+ pass
+
for logrus in pattern.split():
- if logrus.startswith("0x") is True:
+ try:
+ key = server.search(hex(int(logrus, 16)))
+ hexed = True
+ except ValueError as ve:
key = server.search(logrus)
+ hexed = False
+
+ if key is not None:
+ keys.append(key[0])
+ if hexed is True:
+ try:
+ fob = server.search(logrus)
+ except:
+ fob = None
+ if fob is not None:
+ keys.append(fob[0])
+ else:
+ pass
else:
- key = server.search("0x{0}".format(logrus))
- keys.append(key[0])
- print("Found {0} key(s).".format(len(keys)))
+ pass
-for key in keys:
- import_result = c.key_import(key.key_blob)
- results.append(import_result)
+
+if len(keys) > 0:
+ for key in keys:
+ import_result = c.key_import(key.key_blob)
+ results.append(import_result)
for result in results:
if result is not None and hasattr(result, "considered") is False: