summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--passlib/pwd.py172
-rw-r--r--passlib/tests/test_pwd.py102
2 files changed, 9 insertions, 265 deletions
diff --git a/passlib/pwd.py b/passlib/pwd.py
index 02d4652..2a468fc 100644
--- a/passlib/pwd.py
+++ b/passlib/pwd.py
@@ -25,7 +25,6 @@ from __future__ import absolute_import, division, print_function, unicode_litera
# core
import codecs
from collections import defaultdict
-from itertools import chain
from math import ceil, log as logf
import logging; log = logging.getLogger(__name__)
import os
@@ -115,69 +114,6 @@ def _self_info_rate(source):
# return _self_info_rate(source) * len(source)
-def _max_self_info_rate(alphabet_size, output_size):
- """
- calculate maximum _self_info_rate() of all strings of length <output_size>
- created from at most <alphabet_size> symbols (assuming all symbols have equal probability).
- """
- # NOTE: this accomplishes it's purpose by assuming maximum average entropy
- # would be a string repeating all symbols ``output//alphabet`` times,
- # and with ``extra = output % alphabet`` symbols repeated one extra time.
- # the following code then calculates _self_info_rate() for such a string.
-
- # sanity check
- if output_size <= 0:
- if not output_size:
- return 0
- raise ValueError("output size must be positive integer")
- if alphabet_size <= 0:
- raise ValueError("alphabet size must be positive integer")
-
- # all symbols repeated <count> times, and <extra> of them repeated 1 more time.
- count, extra = divmod(output_size, alphabet_size)
- # assert 0 <= extra < alphabet_size
- # assert alphabet_size * count + extra == output_size
-
- # average entropy contributed by head symbols.
- # there are <alphabet-extra> of them (all of alphabet but tail symbols),
- # and each occurs <count> times.
- if count:
- prob_head = count / output_size
- head_bits = (alphabet_size - extra) * prob_head * logf(prob_head, 2)
- else:
- # avoid log(0) error when count=0
- head_bits = 0
-
- # average entropy contributed by tail symbols.
- # there are <extra> of them, and each occurs <count+1> times.
- if extra:
- prob_tail = (count + 1) / output_size
- tail_bits = extra * prob_tail * logf(prob_tail, 2)
- else:
- tail_bits = 0
-
- # combine
- return -(head_bits + tail_bits)
-
-
-# def _self_info_rate_per_char(wordset):
-# """
-# return the average *per-character* entropy in a wordset,
-# using each char's frequency in the wordset as the probability of occurrence.
-#
-# essentially just treats wordset as one long string,
-# and calculates :func:`_self_info_rate`.
-#
-# :arg wordset:
-# iterable containing 1+ words, each of which are themselves
-# iterables containing 1+ characters.
-#
-# :returns:
-# float bits of entropy
-# """
-# return _self_info_rate(chain.from_iterable(wordset))
-
-
#: path to passlib's data dir
_data_dir = os.path.join(os.path.dirname(__file__), "_data")
@@ -195,6 +131,7 @@ def _load_wordset(name):
log.debug("loaded %d-element wordset from %r", len(words), source)
return words
+
def _dup_repr(source):
"""return repr of duplicates in string/list, for use in error message"""
seen = set()
@@ -210,8 +147,6 @@ def _dup_repr(source):
dup_repr += ", ... plus %d others" % (len(dups) - trunc)
return dup_repr
-_MIN_COMPLEXITY_CAP = 1 - 1e-14
-
#=============================================================================
# base generator class
#=============================================================================
@@ -240,14 +175,6 @@ class SequenceGenerator(object):
Also exposed as a readonly attribute.
- :param min_complexity:
- By default, generators derived from this class will avoid
- generating passwords with excessively high per-symbol redundancy
- (e.g. ``aaaaaaaa``). This is done by rejecting any strings
- whose self-information per symbol is below a certain
- percentage of the maximum possible a given string and alphabet
- size. This defaults to 40%, or ``min_complexity=0.4``.
-
.. autoattribute:: length
.. autoattribute:: symbol_count
.. autoattribute:: entropy_per_symbol
@@ -274,16 +201,10 @@ class SequenceGenerator(object):
#: number of potential symbols
symbol_count = None
- #: default threshold for rejecting low entropy sequences,
- #: measured as % of maximum possible average entropy over all
- #: sequences of given output & alphabet size.
- #: TODO: defend this default -- it was picked via experimentation
- min_complexity = 0.1
-
#=============================================================================
# init
#=============================================================================
- def __init__(self, entropy=None, length=None, rng=None, min_complexity=None, **kwds):
+ def __init__(self, entropy=None, length=None, rng=None, **kwds):
# make sure subclass set things up correctly
assert self.symbol_count is not None, "subclass must set .symbol_count"
@@ -305,15 +226,6 @@ class SequenceGenerator(object):
raise ValueError("`length` must be positive integer")
self.length = length
- # init min complexity
- if min_complexity is None:
- min_complexity = self.min_complexity
- if min_complexity < 0 or min_complexity > 1:
- raise ValueError("`min_complexity` must be between 0 and 1")
- self.min_complexity = min_complexity
- # TODO: increase length/entropy to account for min_complexity
- # stripping out some possibilities
-
# init other common options
if rng is not None:
self.rng = rng
@@ -342,43 +254,6 @@ class SequenceGenerator(object):
"""
return self.length * self.entropy_per_symbol
- @memoized_property
- def max_self_info_rate(self):
- """
- maximum self-info rate acheiveable by a sequence <length>
- built from <symbol_count> symbols.
- """
- return _max_self_info_rate(self.symbol_count, self.length)
-
- @memoized_property
- def min_self_info_rate(self):
- """
- minimum entropy allowed in a generated password
- (controlled by min_complexity option)
- """
- # NOTE: MIN_COMPLEXITY_CAP is to prevent edge case where rounding error
- # causes *nothing* to satisfy min_complexity==1
- return min(self.min_complexity, _MIN_COMPLEXITY_CAP) * self.max_self_info_rate
-
- #=============================================================================
- # debugging
- #=============================================================================
- # def _complexity(self, pwd):
- # """
- # measure 'complexity' of generated password
- # """
- # return _average_entropy(pwd) / self.max_self_info_rate
- #
- # def _analyze_complexity(self, count=1):
- # """
- # temporarily helper while exploring additional filters for
- # password generation. looking into forbidding above a certain
- # threshold of redundancy (as minimum % of max possible average entropy)
- # """
- # result = [(p, self._complexity(p)) for p in self(count)]
- # result.sort(key=lambda elem: (elem[1], elem[0]))
- # return result
-
#=============================================================================
# generation
#=============================================================================
@@ -499,12 +374,6 @@ class WordGenerator(SequenceGenerator):
while True:
secret = getrandstr(self.rng, self.chars, self.length)
- # check that it satisfies minimum self-information limit
- # set by min_complexity. i.e., reject strings like "aaaaaaaa"
- # even if they're long enough.
- if _self_info_rate(secret) < self.min_self_info_rate:
- continue
-
# XXX: could do things like optionally ensure character groups
# (e.g. letters & punctuation) are included.
@@ -585,12 +454,6 @@ def genword(entropy=None, length=None, returns=None, **kwds):
#=============================================================================
# pass phrase generator
#=============================================================================
-class JOIN(object):
- """constants for PhraseGenerator join modes"""
- NONE = "none"
- SPACE = "space"
- CAMEL = "camelcase"
-
class PhraseGenerator(SequenceGenerator):
"""class which generates passphrases by randomly choosing
from a list of unique words.
@@ -673,16 +536,6 @@ class PhraseGenerator(SequenceGenerator):
def symbol_count(self):
return len(self.words)
- # @memoized_property
- # def min_chars(self):
- # """
- # minimum number of characters allowed in resulting password.
- # (see comment in ``.__next__()`` for details).
- # """
- # chars_in_secret = (self.length * self.min_self_info_rate /
- # _self_info_rate_per_char(self.words))
- # return int(ceil(chars_in_secret))
-
#=============================================================================
# generation
#=============================================================================
@@ -690,27 +543,10 @@ class PhraseGenerator(SequenceGenerator):
def __next__(self):
while True:
# create random word
- symbols = [self.rng.choice(self.words) for _ in irange(self.length)]
-
- # check that it satisfies minimum self-information limit
- # set by min_complexity. i.e., reject strings like "aaaaaaaa"
- if _self_info_rate(symbols) <= self.min_self_info_rate:
- continue
-
- # XXX: this code isn't quite capturing the right calculation,
- # so omitting this for now.
- # # reject any passwords which contain so many short / repetitive words that it's below
- # # ``chars_in_secret * self_info_rate_per_char < words_in_secret * min_self_info_rate``
- # # ... where self_info_rate_per_char is determined based on frequency of chars in wordset.
- # # To do this cheaply, we precalculate minimum chars_in_secret (above)
- # chars = sum(len(elem) for elem in symbols)
- # if chars < self.min_chars:
- # continue
+ words = (self.rng.choice(self.words) for _ in irange(self.length))
# join using separator
- secret = self.sep.join(symbols)
-
- return secret
+ return self.sep.join(words)
#=============================================================================
# eoc
diff --git a/passlib/tests/test_pwd.py b/passlib/tests/test_pwd.py
index 3b5bbe3..35865f5 100644
--- a/passlib/tests/test_pwd.py
+++ b/passlib/tests/test_pwd.py
@@ -52,50 +52,6 @@ class UtilsTest(TestCase):
# self.assertEqual(_total_self_info("abcd" * 8), 64)
# self.assertAlmostEqual(_total_self_info("abcdaaaa"), 12.3904, places=4)
- def test_max_average_entropy(self):
- """_max_average_entropy()"""
- from passlib.pwd import _max_self_info_rate as _mae, _self_info_rate as _ae
-
- # asize < 1
- self.assertRaises(ValueError, _mae, -1, 1)
- self.assertRaises(ValueError, _mae, 0, 1)
-
- # osize < 1
- self.assertRaises(ValueError, _mae, 4, -1)
- self.assertEqual(_mae(4, 0), 0)
-
- # no repetition (osize <= asize)
- self.assertEqual(_mae(4, 1), 0)
- self.assertEqual(_mae(4, 2), 1) # _ae('ab')
- self.assertAlmostEqual(_mae(4, 3), _ae("abc"), places=5)
- self.assertEqual(_mae(4, 4), 2) # _ae('abcd')
-
- # 1 repetition
- self.assertAlmostEqual(_mae(4, 5), _ae("abcd" + "a"), places=5)
- self.assertAlmostEqual(_mae(4, 6), _ae("abcd" + "ab"), places=5)
- self.assertAlmostEqual(_mae(4, 7), _ae("abcd" + "abc"), places=5)
- self.assertAlmostEqual(_mae(4, 8), _ae("abcd" * 2 ), places=5)
-
- # 2 repetitions
- self.assertAlmostEqual(_mae(4, 9), _ae("abcd" * 2 + "a"), places=5)
- self.assertAlmostEqual(_mae(4, 10), _ae("abcd" * 2 + "ab"), places=5)
- self.assertAlmostEqual(_mae(4, 11), _ae("abcd" * 2 + "abc"), places=5)
- self.assertAlmostEqual(_mae(4, 12), _ae("abcd" * 3), places=5)
-
- # def test_average_entropy_per_wordset_char(self):
- # """_average_entropy_per_wordset_char()"""
- # from passlib.pwd import _self_info_rate_per_char as _awe, _self_info_rate as _ae
- #
- # self.assertEqual(_awe([]), 0)
- # self.assertEqual(_awe(["a"]), 0)
- # self.assertEqual(_awe(["a", "b"]), 1)
- # self.assertEqual(_awe(["a", "b", "c", "d"]), 2)
- #
- # self.assertEqual(_awe(["aa", "bb"]), 1) # a=2/4, b=2/4
- # self.assertEqual(_awe(["ab", "ba"]), 1) # a=2/4, b=2/4
- #
- # self.assertEqual(_awe(["ab", "ba", "ca", "da"]), 1.75) # a=4/8, b=2/8, c=1/8, d=1/8
-
#=============================================================================
# word generation
#=============================================================================
@@ -150,24 +106,12 @@ class WordGeneratorTest(TestCase):
# chars option
# there are 3**3=27 possible combinations
- results = genword(length=3, chars="abc", returns=5000, min_complexity=0)
+ results = genword(length=3, chars="abc", returns=5000)
self.assertResultContents(results, 5000, "abc", unique=27)
# chars + charset
self.assertRaises(TypeError, genword, chars='abc', charset='hex')
- def test_min_complexity(self):
- """'min_complexity' option"""
-
- # test non-zero min_complexity.
- # should reject 'aaa' 'bbb' 'ccc' (0 avg entropy), leaving only 24
- results = genword(length=3, chars="abc", returns=5000, min_complexity=0.001)
- self.assertResultContents(results, 5000, "abc", unique=24)
-
- # test min_complexity=1 -- should only accept permutations of 'abc'
- results = genword(length=3, chars="abc", returns=5000, min_complexity=1)
- self.assertResultContents(results, 5000, "abc", unique=6)
-
# TODO: test rng option
#=============================================================================
@@ -175,9 +119,7 @@ class WordGeneratorTest(TestCase):
#=============================================================================
# import subject
-from passlib.pwd import genphrase, _load_wordset, PhraseGenerator
-default_words = _load_wordset(PhraseGenerator.wordset)
-dice_words = _load_wordset("diceware")
+from passlib.pwd import genphrase, PhraseGenerator
simple_words = ["alpha", "beta", "gamma"]
class PhraseGeneratorTest(TestCase):
@@ -244,45 +186,11 @@ class PhraseGeneratorTest(TestCase):
self.assertResultContents(results, 5000, simple_words)
# words option
- results = genphrase(length=3, words=simple_words, returns=5000, min_complexity=0)
- self.assertResultContents(results, 5000, simple_words, unique=3**3 - 3)
+ results = genphrase(length=3, words=simple_words, returns=5000)
+ self.assertResultContents(results, 5000, simple_words, unique=3**3)
# words + wordset
- self.assertRaises(TypeError, genphrase, words=simple_words, wordset='diceware')
-
- def test_min_complexity(self):
- """'min_complexity' option"""
-
- # test non-zero min_complexity.
- # should reject repeats of 'alpha alpha alpha', etc (0 avg entropy)
- results = genphrase(length=3, words=simple_words, returns=1000, min_complexity=0.001)
- self.assertResultContents(results, 1000, simple_words, unique=3**3 - 3)
-
- # XXX: trusting this will work, it does for charsets --
- # but min_chars requirement makes this take forever
- # # test min_complexity=1 -- should only accept permutations containing all symbols
- # results = genphrase(length=3, words=simple_words, returns=24, min_complexity=1)
- # self.assertResultContents(results, 24, simple_words, unique=6) # =3!
-
- # def test_min_chars(self):
- # """min_chars protection"""
- # from passlib.pwd import _self_info_rate_per_char
- #
- # # sanity check our wordset
- # entropy_per_char = _self_info_rate_per_char(simple_words)
- # self.assertAlmostEqual(entropy_per_char, 2.8352, places=4)
- #
- # # create generator
- # gen = PhraseGenerator(entropy=48, words=simple_words, sep="")
- #
- # # sanity check: gen should target nearest multiple of entropy_per_symbol >= 48
- # self.assertEqual(gen.entropy, 48)
- # self.assertAlmostEqual(gen.effective_entropy, 49.1338, places=4)
- # self.assertEqual(gen.length, 31) # 48 / 1.58496
- #
- # # given numbers above, anything less than 49.1338 / 2.8352 could be guessed
- # # using a character based attack, w/o needing wordset
- # self.assertEqual(gen.min_chars, 2)
+ self.assertRaises(TypeError, genphrase, words=simple_words, wordset='bip39')
#=============================================================================
# strength