diff options
author | Lars Wirzenius <liw@liw.fi> | 2016-12-31 22:10:12 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2016-12-31 22:10:12 +0200 |
commit | 9e4095e8b75f7235ea5dfef48f06cfef116692d3 (patch) | |
tree | 79be1b0560404712a4ce35935aa519d473cb2df0 | |
parent | 23683ca2bc1d9dd07e4ebee6ba348a5e7eaee1bb (diff) | |
download | py_pgpwordlist-9e4095e8b75f7235ea5dfef48f06cfef116692d3.tar.gz |
Add hex_to_words and words_to_hex
-rw-r--r-- | pgpwordlist/__init__.py | 3 | ||||
-rw-r--r-- | pgpwordlist/funcs.py | 14 | ||||
-rw-r--r-- | pgpwordlist/funcs_tests.py | 8 |
3 files changed, 24 insertions, 1 deletions
diff --git a/pgpwordlist/__init__.py b/pgpwordlist/__init__.py index 495cd51..ceb321c 100644 --- a/pgpwordlist/__init__.py +++ b/pgpwordlist/__init__.py @@ -17,4 +17,5 @@ from .wordlist import pgp_word_list -from .funcs import get_word, get_hex +from .funcs import get_word, get_hex, hex_to_words, words_to_hex + diff --git a/pgpwordlist/funcs.py b/pgpwordlist/funcs.py index 2801839..84d3d0d 100644 --- a/pgpwordlist/funcs.py +++ b/pgpwordlist/funcs.py @@ -33,3 +33,17 @@ def get_hex(word): for hex, t in _words.items(): if word.lower() in t: return hex.lower() + + +def hex_to_words(hexstr): + words = [get_word(hex, i) for i, hex in enumerate(_hex_bytes(hexstr))] + return ' '.join(words) + + +def _hex_bytes(hexstr): + for i in range(0, len(hexstr), 2): + yield hexstr[i:i+2] + + +def words_to_hex(wordstr): + return ''.join(get_hex(w) for w in wordstr.split()) diff --git a/pgpwordlist/funcs_tests.py b/pgpwordlist/funcs_tests.py index a7d1bfb..5473ac8 100644 --- a/pgpwordlist/funcs_tests.py +++ b/pgpwordlist/funcs_tests.py @@ -46,3 +46,11 @@ class WordListQueriesTests(unittest.TestCase): odd_word = pgpwordlist.get_word(hex, 1) self.assertEqual(pgpwordlist.get_hex(even_word), hex) self.assertEqual(pgpwordlist.get_hex(odd_word), hex) + + +class WordStringTests(unittest.TestCase): + + def test_roundtrip_works(self): + hex = 'cafef00d' + words = pgpwordlist.hex_to_words(hex) + self.assertEqual(pgpwordlist.words_to_hex(words), hex) |