summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pgpwordlist/__init__.py2
-rw-r--r--pgpwordlist/funcs.py10
-rw-r--r--pgpwordlist/funcs_tests.py7
3 files changed, 17 insertions, 2 deletions
diff --git a/pgpwordlist/__init__.py b/pgpwordlist/__init__.py
index 61ca1d4..495cd51 100644
--- a/pgpwordlist/__init__.py
+++ b/pgpwordlist/__init__.py
@@ -17,4 +17,4 @@
from .wordlist import pgp_word_list
-from .funcs import get_word
+from .funcs import get_word, get_hex
diff --git a/pgpwordlist/funcs.py b/pgpwordlist/funcs.py
index 59a5972..2801839 100644
--- a/pgpwordlist/funcs.py
+++ b/pgpwordlist/funcs.py
@@ -18,10 +18,18 @@
import pgpwordlist
+_words = pgpwordlist.pgp_word_list
+
def get_word(hex, offset):
hex = hex.lower()
- t = pgpwordlist.pgp_word_list.get(hex)
+ t = _words.get(hex)
if t is None:
raise KeyError(hex)
return t[offset % 2]
+
+
+def get_hex(word):
+ for hex, t in _words.items():
+ if word.lower() in t:
+ return hex.lower()
diff --git a/pgpwordlist/funcs_tests.py b/pgpwordlist/funcs_tests.py
index e4f6805..a7d1bfb 100644
--- a/pgpwordlist/funcs_tests.py
+++ b/pgpwordlist/funcs_tests.py
@@ -39,3 +39,10 @@ class WordListQueriesTests(unittest.TestCase):
even_words.add(even_word)
odd_words.add(odd_word)
+ def test_round_trip_works(self):
+ for i in range(256):
+ hex = '%02x' % i
+ even_word = pgpwordlist.get_word(hex, 0)
+ odd_word = pgpwordlist.get_word(hex, 1)
+ self.assertEqual(pgpwordlist.get_hex(even_word), hex)
+ self.assertEqual(pgpwordlist.get_hex(odd_word), hex)