From e282d8168541eb30c1dd9d1f1e43833602e16760 Mon Sep 17 00:00:00 2001 From: Donald Nguyen Date: Sun, 20 Oct 2019 15:55:52 -0400 Subject: [PATCH 1/2] Add option -m/--max --- README.rst | 2 ++ diceware/__init__.py | 5 +++++ diceware/config.py | 1 + tests/sample_dot_diceware.ini | 1 + 4 files changed, 9 insertions(+) diff --git a/README.rst b/README.rst index e11cd06..aed2ac1 100644 --- a/README.rst +++ b/README.rst @@ -71,6 +71,8 @@ Once installed, use ``--help`` to list all available options:: --no-caps Turn off capitalization. -s NUM, --specials NUM Insert NUM special chars into generated word. + -m NUM, --max NUM + Truncate to NUM length. -d DELIMITER, --delimiter DELIMITER Separate words by DELIMITER. Empty string by default. -r SOURCE, --randomsource SOURCE diff --git a/diceware/__init__.py b/diceware/__init__.py index d034c43..40b3694 100644 --- a/diceware/__init__.py +++ b/diceware/__init__.py @@ -104,6 +104,9 @@ def handle_options(args): parser.add_argument( '-s', '--specials', default=0, type=int, metavar='NUM', help="Insert NUM special chars into generated word.") + parser.add_argument( + '-m', '--max', default=0, type=int, metavar='NUM', + help="Truncate to NUM length.") parser.add_argument( '-d', '--delimiter', default='', help="Separate words by DELIMITER. Empty string by default.") @@ -193,6 +196,8 @@ def get_passphrase(options=None): result = options.delimiter.join(words) for _ in range(options.specials): result = insert_special_char(result, rnd=rnd) + if options.max > 0: + result = result[:options.max] return result diff --git a/diceware/config.py b/diceware/config.py index ae1f7b9..3e5a674 100644 --- a/diceware/config.py +++ b/diceware/config.py @@ -30,6 +30,7 @@ num=6, caps=True, specials=0, + max=0, delimiter="", randomsource="system", verbose=0, diff --git a/tests/sample_dot_diceware.ini b/tests/sample_dot_diceware.ini index 01e430c..9111f08 100644 --- a/tests/sample_dot_diceware.ini +++ b/tests/sample_dot_diceware.ini @@ -3,6 +3,7 @@ num = 6 caps = on specials = 0 delimiter = "" +max = 0 randomsource = "system" verbose = 0 wordlist = "en_securedrop" From fd7714564e82481bc9aedc08c3bf41f140b404ef Mon Sep 17 00:00:00 2001 From: Donald Nguyen Date: Sun, 20 Oct 2019 16:34:16 -0400 Subject: [PATCH 2/2] Added test for max --- tests/test_diceware.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_diceware.py b/tests/test_diceware.py index 319e919..2d8306f 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -45,6 +45,7 @@ def test_handle_options_defaults(self): assert options.randomsource == "system" assert options.wordlist == "en_eff" assert options.verbose == 0 + assert options.max == 0 def test_handle_options_infile(self, tmpdir): # we can give an infile @@ -69,6 +70,14 @@ def test_handle_options_caps(self): assert options.caps is True options = handle_options(['--no-caps', ]) assert options.caps is False + + def test_handle_options_max(self): + options = handle_options([]) + assert options.max == 0 + options = handle_options(['-m', '0']) + assert options.max == 0 + options = handle_options(['--max', '0']) + assert options.max == 0 def test_handle_options_caps_conflicting_raises_exc(self): # conflicting caps-settings raise an exception @@ -243,6 +252,12 @@ def test_get_passphrase_delimiters(self): options.delimiter = " " phrase = get_passphrase(options) assert " " in phrase + + def test_get_passphrase_max(self): + options = handle_options(args=[]) + options.max = 15 + phrase = get_passphrase(options) + assert len(phrase) == 15 def test_print_version(self, capsys): # we can print version infos @@ -362,4 +377,4 @@ def test_main_wordlist(self, argv_handler, capsys, wordlists_dir): sys.argv = ['diceware', '-w', 'foo'] main() out, err = capsys.readouterr() - assert out == 'FooFooFooFooFooFoo\n' + assert out == 'FooFooFooFooFooFoo\n' \ No newline at end of file