patternpythonMinor
Diceware Passphrase Generator
Viewed 0 times
dicewarepassphrasegenerator
Problem
I've written a simple Diceware passphrase generator based on this Stack Overflow question and the top answer.
Briefly, in the Diceware method, a die is rolled 5 times for each word in the passphrase and the results matched to the corresponding word in the word list which is of the form:
I'm still very much a Python and programming beginner and appreciate any feedback, but my main concerns are:
`import os
import sys
WORDS = "diceware_wordlist.txt"
def exists_arg():
"""Checks if argv input was given."""
if len(sys.argv) == 1:
return False
else:
return True
def test_arg(arg):
"""Checks if input is of the required form and returns
various failure cases.
"""
try:
arg = int(arg)
except ValueError:
return 'Please enter an integer.'
if arg = 5.'
elif arg > 10:
return 'Length must be
Briefly, in the Diceware method, a die is rolled 5 times for each word in the passphrase and the results matched to the corresponding word in the word list which is of the form:
16226 cask
16231 casket
16232 cast
16233 caste
16234 cat
16235 catch
I'm still very much a Python and programming beginner and appreciate any feedback, but my main concerns are:
- Is the method I use to generate keys biased?
- Is my handling of command line arguments correct or could it be done better?
- Is the way I've broken my code down into individual functions reasonable or should certain functions be combined or broken apart?
- Are there any other glaring weaknesses?
`import os
import sys
WORDS = "diceware_wordlist.txt"
def exists_arg():
"""Checks if argv input was given."""
if len(sys.argv) == 1:
return False
else:
return True
def test_arg(arg):
"""Checks if input is of the required form and returns
various failure cases.
"""
try:
arg = int(arg)
except ValueError:
return 'Please enter an integer.'
if arg = 5.'
elif arg > 10:
return 'Length must be
Solution
- Is my handling of command line arguments correct or could it be done better?
It would be better to not reinvent the wheel and use
argparse instead.- Is the way I've broken my code down into individual functions reasonable or should certain functions be combined or broken apart?
It's quite fine the way you did it.
Every function seems to do just one thing, it's good like that.
- Are there any other glaring weaknesses?
Instead of this:
if len(sys.argv) == 1:
return False
else:
return TrueWrite like this:
return len(sys.argv) != 1Instead of this:
with open(WORDS, 'r', 0) as in_file:I'd recommend simply:
with open(WORDS) as in_file:Instead of this:
for i in range(1, length + 1):
key = generate_key()
passphrase += word_dict[key] + ' 'This is simpler:
for _ in range(length):
key = generate_key()
passphrase += word_dict[key] + ' 'I replaced the loop variable
i with _,because it's a common convention when the loop variable is not used.
Lastly,
I recommend to switch to start using Python 3 instead of 2.7.
Code Snippets
if len(sys.argv) == 1:
return False
else:
return Truereturn len(sys.argv) != 1with open(WORDS, 'r', 0) as in_file:with open(WORDS) as in_file:for i in range(1, length + 1):
key = generate_key()
passphrase += word_dict[key] + ' 'Context
StackExchange Code Review Q#91527, answer score: 2
Revisions (0)
No revisions yet.