Re:How To Safely Store A Password
by iki23 on Friday July 01, 2011 @04:24AM
(#2717508)
You can also initialize the words in more pythonic way:
Or, for one time check you can stop reading on match:
As for the objective, this checks only exact dictionary matches. In real world, you'd use cracklib to check any dictionary based or weak passwords rather than reinvent the wheel. See http://gdub.wordpress.com/2006/08/26/using-cracklib-to-requi...
[ Parent
| on HN ]
While I agree that the first code is pythonic and nice, the second code is quite the opposite of that. Why forcing imperative code into a list comprehension? It is a lot easier to read as nested loop, as it doesn't build a dummy list with some strange "or" operation:
import sys
lookup = '%s\n' % sys.argv[1]
with open('/usr/share/dict/words') as wordlist:
for line in wordlist:
if line == lookup:
sys.stdout.write('Dictionary password: %s' % lookup)
sys.exit(1)
Re:How To Safely Store A Password
by nerfhammer on Friday July 01, 2011 @11:09AM
(#2718611)
better to pickle a frozenset and use that.
import pickle
# initialize:
wordset = frozenset(line.lower().rstrip() for line in open('/usr/share/dict/words'))
pickle.dump(wordset, open('/tmp/wordset.pkl', 'wb', -1))
# when you want to use it:
wordset = pickle.load(open('/tmp/wordset.pkl', 'rb'))
'bear' in wordset # == TrueThanks for the frozenset. Yet pickle makes the file 2 times bigger and 20 times slower to parse on my machine:
>python wordlist.py -w wordlist -p wordlist.pkl
>python wordlist.py -T wordlist wordlist.pkl 10 3
2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
words(wordfile): 142.50 best msec/loop
words_slower(wordfile): 161.00 best msec/loop
words_normalized(wordfile): 295.84 best msec/loop
words_pickled(pickled): 2943.66 best msec/loop
The winner is: with open(wordfile) as wordlist:
return frozenset(wordlist.read().split('\n'))
Source: https://gist.github.com/1059725Wordlist: http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?re...
Do you have different results?