Skip to content

Commit

Permalink
verbose_reports: Add support for correct entropy for padded digits.
Browse files Browse the repository at this point in the history
I have added the correct entropy information for when having padded
digits in the passphrase.

Fixes #5.
  • Loading branch information
adambirds committed May 2, 2021
1 parent f9ccdb1 commit c58eeef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
30 changes: 22 additions & 8 deletions src/xkcd_pass/xkcd_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,30 @@ def verbose_reports(wordlist: List[str], options: argparse.Namespace) -> None:
length = len(wordlist)
numwords = options.numwords

bits = math.log(length, 2)
if options.no_padding_digits == False:
length = length + 9
numwords = numwords + options.padding_digits_num
bits = math.log(length, 2)
print("With the current options, your word list contains {0} words.".format(len(wordlist)))

print("With the current options, your word list contains {0} words.".format(length))
print(
"A {0} word password from this list with {1} appending digits will have roughly "
"{2} ({3:.3f} * {4}) bits of entropy,"
"".format(
options.numwords, options.padding_digits_num, int(bits * numwords), bits, numwords
)
)
print("assuming truly random word selection.\n")
else:
bits = math.log(length, 2)
print("With the current options, your word list contains {0} words.".format(length))

print(
"A {0} word password from this list will have roughly "
"{1} ({2:.2f} * {3}) bits of entropy,"
"".format(numwords, int(bits * numwords), bits, numwords)
)
print("assuming truly random word selection.\n")
print(
"A {0} word password from this list will have roughly "
"{1} ({2:.2f} * {3}) bits of entropy,"
"".format(numwords, int(bits * numwords), bits, numwords)
)
print("assuming truly random word selection.\n")


def choose_words(wordlist: List[str], numwords: int) -> List[str]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_xkcd_pass_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_main_verbose(self) -> None:
with self.stdout_patcher as mock_stdout:
xkcd_pass.main()
output = mock_stdout.getvalue()
self.assertEqual(len(output.strip()), 199)
self.assertEqual(len(output.strip()), 225)

def test_main_interactive(self) -> None:
"""
Expand Down
22 changes: 20 additions & 2 deletions tests/test_xkcd_pass_verbose_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,37 @@ def setUp(self) -> None:
self.options = argparse.Namespace(
numwords=6,
verbose=True,
no_padding_digits=False,
padding_digits_num=2,
)

self.stdout_patcher = mock.patch.object(sys, "stdout", new_callable=io.StringIO)

def test_verbose_output(self) -> None:
def test_verbose_output_with_padding_digits(self) -> None:
"""
Should display verbose reporting.
Should display verbose reporting with padding digits.
"""
with self.stdout_patcher as mock_stdout:
xkcd_pass.verbose_reports(wordlist=self.wordlist_small, options=self.options)
output = mock_stdout.getvalue()
expected_output = """
With the current options, your word list contains 6 words.
A 6 word password from this list with 2 appending digits will have roughly 31 (3.907 * 8) bits of entropy,
assuming truly random word selection.
""".strip()
self.assertEqual(output.strip(), expected_output)

def test_verbose_output_without_padding_digits(self) -> None:
"""
Should display verbose reporting without padding digits.
"""
self.options.no_padding_digits = True
self.options.padding_digits_num = None
with self.stdout_patcher as mock_stdout:
xkcd_pass.verbose_reports(wordlist=self.wordlist_small, options=self.options)
output = mock_stdout.getvalue()
expected_output = """
With the current options, your word list contains 6 words.
A 6 word password from this list will have roughly 15 (2.58 * 6) bits of entropy,
assuming truly random word selection.
""".strip()
Expand Down

0 comments on commit c58eeef

Please sign in to comment.