diff --git a/diceware/wordlist.py b/diceware/wordlist.py index 49cd5a87be4dbfae83480c6046dd3d478accac5b..77caee0e0e29eaf215ca4ae5fb990ebbfbb32ebf 100644 --- a/diceware/wordlist.py +++ b/diceware/wordlist.py @@ -45,6 +45,16 @@ def get_wordlist_names(): return sorted(result) +def is_signed_wordlist(file_descriptor): + """check, whether file in `file_descriptor` is signed. + """ + line1 = file_descriptor.readline() + file_descriptor.seek(0) + if line1.rstrip() == "-----BEGIN PGP SIGNED MESSAGE-----": + return True + return False + + def get_wordlist(file_descriptor): """Parse file in `file_descriptor` and build a word list of it. diff --git a/tests/test_wordlist.py b/tests/test_wordlist.py index dd20976746a9f485dec4443a57ee091800718883..1815b386ddfbd4b4a00dd1d781b4aa499c03832b 100644 --- a/tests/test_wordlist.py +++ b/tests/test_wordlist.py @@ -2,7 +2,7 @@ import os import pytest from diceware.wordlist import ( WORDLISTS_DIR, RE_WORDLIST_NAME, get_wordlist, get_signed_wordlist, - get_wordlist_path, get_wordlist_names, + get_wordlist_path, get_wordlist_names, is_signed_wordlist, ) @@ -114,3 +114,19 @@ class TestWordlistModule(object): # we only recognize wordlist files with dot in name wordlists_dir.join("file_without_dot-in-name").write("a\nb\n") assert get_wordlist_names() == [] + + def test_is_signed_wordlist(self): + # we recognize signed wordlists + in_path = os.path.join( + os.path.dirname(__file__), "sample_signed_wordlist.asc") + with open(in_path, "r") as fd: + result = is_signed_wordlist(fd) + assert result is True + + def test_is_signed_wordlist_plain(self, tmpdir): + # we can tell if a wordlist is not signed + in_file = tmpdir.mkdir("work").join("mywordlist") + in_file.write("a\nb\n") + with open(in_file.strpath, 'r') as fd: + result = is_signed_wordlist(fd) + assert result is False