From 686a07b3a71723a680a62bdf2877116392eb7e89 Mon Sep 17 00:00:00 2001 From: Hartmut Goebel Date: Thu, 25 Apr 2019 15:39:56 +0200 Subject: [PATCH] Add option -D/--delimiter-special. --- CHANGES.rst | 5 ++++- README.rst | 5 +++++ diceware/__init__.py | 13 ++++++++++++- diceware/config.py | 1 + tests/sample_dot_diceware.ini | 1 + 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 4bbc32f..2e1c1f4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,10 @@ Changes 0.9.7.dev0 (yet unreleased) --------------------------- -(no changes yet) +- Added option ``--delimiter-special``. Thanks to `Hartmut Goebel + `_. +- Added German, Italien, Latin and two Spanish wordlists. Thanks to + `Hartmut Goebel `_. 0.9.6 (2018-12-19) ------------------ diff --git a/README.rst b/README.rst index e11cd06..c38ff30 100644 --- a/README.rst +++ b/README.rst @@ -73,6 +73,8 @@ Once installed, use ``--help`` to list all available options:: Insert NUM special chars into generated word. -d DELIMITER, --delimiter DELIMITER Separate words by DELIMITER. Empty string by default. + -D NUM, --delimiter-special NUM + Separate words by NUM special chars (none by default). -r SOURCE, --randomsource SOURCE Get randomness from this source. Possible values: `realdice', `system'. Default: system @@ -208,6 +210,7 @@ directory. This file could look like this:: caps = off specials = 2 delimiter = "MYDELIMITER" + delimiter_special = 0 randomsource = "system" wordlist = "en_securedrop" @@ -412,6 +415,8 @@ People that helped spotting bugs, providing solutions, etc.: - `Simon Fondrie-Teitler `_ contributed a machine-readable copyright file, with improvements from `@anarcat`_ - `Doug Muth `_ fixed formatting in docs. + - `Hartmut Goebel `_ added option + ``--delimiter-special`` and some wordlists. Many thanks to all of them! diff --git a/diceware/__init__.py b/diceware/__init__.py index d034c43..e9002b1 100644 --- a/diceware/__init__.py +++ b/diceware/__init__.py @@ -107,6 +107,9 @@ def handle_options(args): parser.add_argument( '-d', '--delimiter', default='', help="Separate words by DELIMITER. Empty string by default.") + parser.add_argument( + '-D', '--delimiter-special', default=0, type=int, metavar='NUM', + help="Separate words by NUM special chars (none by default).") parser.add_argument( '-r', '--randomsource', default='system', choices=random_sources, metavar="SOURCE", @@ -190,7 +193,15 @@ def get_passphrase(options=None): words = [rnd.choice(list(word_list)) for x in range(options.num)] if options.caps: words = [x.capitalize() for x in words] - result = options.delimiter.join(words) + if options.delimiter_special: + lengths = list(range(1, options.delimiter_special + 1)) + for pos in range(len(words)-1, 0, -1): + l = rnd.choice(lengths) + deli = "".join(rnd.choice(SPECIAL_CHARS) for j in range(l)) + words.insert(pos, deli) + result = "".join(words) + else: + result = options.delimiter.join(words) for _ in range(options.specials): result = insert_special_char(result, rnd=rnd) return result diff --git a/diceware/config.py b/diceware/config.py index ae1f7b9..c07078e 100644 --- a/diceware/config.py +++ b/diceware/config.py @@ -31,6 +31,7 @@ caps=True, specials=0, delimiter="", + delimiter_special=0, randomsource="system", verbose=0, wordlist="en_eff", diff --git a/tests/sample_dot_diceware.ini b/tests/sample_dot_diceware.ini index 01e430c..1c86ef0 100644 --- a/tests/sample_dot_diceware.ini +++ b/tests/sample_dot_diceware.ini @@ -3,6 +3,7 @@ num = 6 caps = on specials = 0 delimiter = "" +delimiter_special = 0 randomsource = "system" verbose = 0 wordlist = "en_securedrop"