diff --git a/diceware/wordlist.py b/diceware/wordlist.py index 210c2674ec1946f3b16acc876f55e3b3107654cf..df5e64f38f7d7d332011f585e9e527faf0fcebc1 100644 --- a/diceware/wordlist.py +++ b/diceware/wordlist.py @@ -63,6 +63,33 @@ def get_wordlist(file_descriptor): return result +def get_signed_wordlist(file_descriptor): + """Parse cryptographically signed file in `file_descriptor` and + build a wordlist out of it. + + `file_descriptor` is expected to be a file descriptor, already + opened for reading. The descriptor will be closed after + processing. + + Signed wordlists are expected to be wordlists as described in + `get_wordlist()` but with ASCII armored signatures (as described in + RFC 4880). + + The signature headers/footers are stripped and the contained list of + words returned. + """ + result = [] + while file_descriptor.readline().strip(): + pass + for line in file_descriptor.readlines(): + line = line.strip() + if line == '-----BEGIN PGP SIGNATURE-----': + break + result += [line.strip(), ] + file_descriptor.close() + return result + + def get_wordlist_path(name): """Get path to a wordlist file for a wordlist named `name`. diff --git a/tests/test_wordlist.py b/tests/test_wordlist.py index aa57fb4c75ce0e6a8d02cf80ae68c52efba74fcd..8b4be0ed574d6491665f1ef03fda01297b757447 100644 --- a/tests/test_wordlist.py +++ b/tests/test_wordlist.py @@ -1,8 +1,8 @@ import os import pytest from diceware.wordlist import ( - WORDLISTS_DIR, RE_WORDLIST_NAME, get_wordlist, get_wordlist_path, - get_wordlist_names, + WORDLISTS_DIR, RE_WORDLIST_NAME, get_wordlist, get_signed_wordlist, + get_wordlist_path, get_wordlist_names, ) @@ -50,6 +50,17 @@ class Test_GetWordList(object): assert fd.closed is True +class Test_GetSignedWordList(object): + + def test_get_signed_wordlist_handles_clearsigned_files(self, tmpdir): + # we can process cryptogrphically signed files + in_path = os.path.join( + os.path.dirname(__file__), "sample_signed_wordlist.asc") + with open(in_path, 'r') as fd: + result = get_signed_wordlist(fd) + assert ["foo", "bar"] == result + + class TestWordlistModule(object): def test_re_wordlist_name(self):