diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2019-11-10 14:56:25 -0500 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2019-11-10 14:56:25 -0500 |
| commit | 8c3470170628cbf6b18b48d95a69800b79b327ec (patch) | |
| tree | 0ad961aec90b15c9e1e849c1f21c1849ca4c2191 /passlib/utils | |
| parent | c8b36453db3bcfb70017fe79d50c4c461d18e161 (diff) | |
| parent | a945d60e814337e668c647a043bfd6adcbd9d47e (diff) | |
| download | passlib-8c3470170628cbf6b18b48d95a69800b79b327ec.tar.gz | |
Merge from stable
Diffstat (limited to 'passlib/utils')
| -rw-r--r-- | passlib/utils/__init__.py | 24 | ||||
| -rw-r--r-- | passlib/utils/handlers.py | 23 |
2 files changed, 32 insertions, 15 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index e879118..2fc0e95 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -6,7 +6,13 @@ from passlib.utils.compat import JYTHON # core from binascii import b2a_base64, a2b_base64, Error as _BinAsciiError from base64 import b64encode, b64decode -import collections +try: + from collections.abc import Sequence + from collections.abc import Iterable +except ImportError: + # py2 compat + from collections import Sequence + from collections import Iterable from codecs import lookup as _lookup_codec from functools import update_wrapper import itertools @@ -30,6 +36,7 @@ else: import time if stringprep: import unicodedata +import timeit import types from warnings import warn # site @@ -272,14 +279,14 @@ def batch(source, size): """ if size < 1: raise ValueError("size must be positive integer") - if isinstance(source, collections.Sequence): + if isinstance(source, Sequence): end = len(source) i = 0 while i < end: n = i + size yield source[i:n] i = n - elif isinstance(source, collections.Iterable): + elif isinstance(source, Iterable): itr = iter(source) while True: chunk_itr = itertools.islice(itr, size) @@ -836,14 +843,7 @@ def test_crypt(secret, hash): assert secret and hash return safe_crypt(secret, hash) == hash -# pick best timer function to expose as "tick" - lifted from timeit module. -if sys.platform == "win32": - # On Windows, the best timer is time.clock() - from time import clock as timer -else: - # On most other platforms the best timer is time.time() - from time import time as timer - +timer = timeit.default_timer # legacy alias, will be removed in passlib 2.0 tick = timer @@ -900,7 +900,7 @@ def genseed(value=None): # the current time, to whatever precision os uses time.time(), - time.clock(), + tick(), # if urandom available, might as well mix some bytes in. os.urandom(32).decode("latin-1") if has_urandom else 0, diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py index cc9d151..50440de 100644 --- a/passlib/utils/handlers.py +++ b/passlib/utils/handlers.py @@ -811,13 +811,25 @@ class GenericHandler(MinimalHandler): # experimental - the following methods are not finished or tested, # but way work correctly for some hashes #=================================================================== + + #: internal helper for forcing settings to be included, even if default matches + _always_parse_settings = () + + #: internal helper for excluding certain setting_kwds from parsehash() result _unparsed_settings = ("salt_size", "relaxed") + + #: parsehash() keys that need to be sanitized _unsafe_settings = ("salt", "checksum") @classproperty def _parsed_settings(cls): - return (key for key in cls.setting_kwds - if key not in cls._unparsed_settings) + """ + helper for :meth:`parsehash` -- + returns list of attributes which should be extracted by parse_hash() from hasher object. + + default implementation just takes setting_kwds, and excludes _unparsed_settings + """ + return tuple(key for key in cls.setting_kwds if key not in cls._unparsed_settings) # XXX: make this a global function? @staticmethod @@ -857,9 +869,13 @@ class GenericHandler(MinimalHandler): self = cls.from_string(hash) # XXX: could split next few lines out as self._parsehash() for subclassing # XXX: could try to resolve ident/variant to publically suitable alias. + # XXX: for v1.8, consider making "always" the default policy, and compare to class default + # only for whitelisted attrs? or make this whole method obsolete by reworking + # so "hasher" object & it's attrs are public? UNSET = object() + always = self._always_parse_settings kwds = dict((key, getattr(self, key)) for key in self._parsed_settings - if getattr(self, key) != getattr(cls, key, UNSET)) + if key in always or getattr(self, key) != getattr(cls, key, UNSET)) if checksum and self.checksum is not None: kwds['checksum'] = self.checksum if sanitize: @@ -1114,6 +1130,7 @@ class HasManyIdents(GenericHandler): return value # failure! + # XXX: give this it's own error type? raise ValueError("invalid ident: %r" % (ident,)) #=================================================================== |
