From f20dc3a6271f6b584d4fc6e16c30da4624cfb027 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 31 Dec 2016 23:44:00 +0200 Subject: Use reverse lookup table for word to hex lookup --- pgpwordlist/funcs.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pgpwordlist/funcs.py b/pgpwordlist/funcs.py index 4e11b9f..37a17be 100644 --- a/pgpwordlist/funcs.py +++ b/pgpwordlist/funcs.py @@ -21,6 +21,17 @@ import pgpwordlist _words = pgpwordlist.pgp_word_list +def _make_reverse_lookup(words): + rev = {} + for hexstr, t in words.items(): + rev[t[0]] = hexstr + rev[t[1]] = hexstr + return rev + + +_w2hex = _make_reverse_lookup(_words) + + def get_word(hexstr, offset): hexstr = hexstr.lower() t = _words.get(hexstr) @@ -30,9 +41,7 @@ def get_word(hexstr, offset): def get_hex(word): - for hexstr, t in _words.items(): - if word.lower() in t: - return hexstr.lower() + return _w2hex[word.lower()] def hex_to_words(hexstr): -- cgit v1.2.1