diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2013-05-06 17:03:23 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2013-05-06 17:03:23 -0400 |
| commit | 306f9ba07c7832aeffac0ade60f0c55f51ac0959 (patch) | |
| tree | 8c0e2512586b8ba688eb5ccf7e61827fce833500 /passlib/utils/__init__.py | |
| parent | ba541d2760452509fd12a765c9b0fd5385fcdbc5 (diff) | |
| parent | 8bcfde1a73c7aac7ed9185804db272f13ea8cafb (diff) | |
| download | passlib-306f9ba07c7832aeffac0ade60f0c55f51ac0959.tar.gz | |
Merge password generation helpers into default (not completed, but will be before 1.7 release)
Diffstat (limited to 'passlib/utils/__init__.py')
| -rw-r--r-- | passlib/utils/__init__.py | 79 |
1 files changed, 44 insertions, 35 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index 9cd3cda..de3e9f2 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -12,6 +12,7 @@ import math import os import sys import random +import re if JYTHON: # pragma: no cover -- runtime detection # Jython 2.5.2 lacks stringprep module - # see http://bugs.jython.org/issue1758320 @@ -130,7 +131,7 @@ class classproperty(object): @property def __func__(self): - "py3 compatible alias" + """py3 compatible alias""" return self.im_func def deprecated_function(msg=None, deprecated=None, removed=None, updoc=True, @@ -221,7 +222,7 @@ class memoized_property(object): @property def __func__(self): - "py3 alias" + """py3 alias""" return self.im_func # works but not used @@ -253,7 +254,7 @@ def consteq(left, right): The purpose of this function is to help prevent timing attacks during digest comparisons: the standard ``==`` operator aborts - after the first mismatched character, causing it's runtime to be + after the first mismatched character, causing its runtime to be proportional to the longest prefix shared by the two inputs. If an attacker is able to predict and control one of the two inputs, repeated queries can be leveraged to reveal information about @@ -456,7 +457,7 @@ def saslprep(source, param="value"): # replace saslprep() with stub when stringprep is missing if stringprep is None: # pragma: no cover -- runtime detection def saslprep(source, param="value"): - "stub for saslprep()" + """stub for saslprep()""" raise NotImplementedError("saslprep() support requires the 'stringprep' " "module, which is " + _stringprep_missing_reason) @@ -503,11 +504,11 @@ add_doc(bytes_to_int, "decode byte string as single big-endian integer") add_doc(int_to_bytes, "encode integer as single big-endian byte string") def xor_bytes(left, right): - "Perform bitwise-xor of two byte strings (must be same size)" + """Perform bitwise-xor of two byte strings (must be same size)""" return int_to_bytes(bytes_to_int(left) ^ bytes_to_int(right), len(left)) def repeat_string(source, size): - "repeat or truncate <source> string, so it has length <size>" + """repeat or truncate <source> string, so it has length <size>""" cur = len(source) if size > cur: mult = (size+cur-1)//cur @@ -519,7 +520,7 @@ _BNULL = b("\x00") _UNULL = u("\x00") def right_pad_string(source, size, pad=None): - "right-pad or truncate <source> string, so it has length <size>" + """right-pad or truncate <source> string, so it has length <size>""" cur = len(source) if size > cur: if pad is None: @@ -535,11 +536,11 @@ _ASCII_TEST_BYTES = b("\x00\n aA:#!\x7f") _ASCII_TEST_UNICODE = _ASCII_TEST_BYTES.decode("ascii") def is_ascii_codec(codec): - "Test if codec is compatible with 7-bit ascii (e.g. latin-1, utf-8; but not utf-16)" + """Test if codec is compatible with 7-bit ascii (e.g. latin-1, utf-8; but not utf-16)""" return _ASCII_TEST_UNICODE.encode(codec) == _ASCII_TEST_BYTES def is_same_codec(left, right): - "Check if two codec names are aliases for same codec" + """Check if two codec names are aliases for same codec""" if left == right: return True if not (left and right): @@ -549,7 +550,7 @@ def is_same_codec(left, right): _B80 = b('\x80')[0] _U80 = u('\x80') def is_ascii_safe(source): - "Check if string (bytes or unicode) contains only 7-bit ascii" + """Check if string (bytes or unicode) contains only 7-bit ascii""" r = _B80 if isinstance(source, bytes) else _U80 return all(c < r for c in source) @@ -656,7 +657,7 @@ add_doc(to_native_str, @deprecated_function(deprecated="1.6", removed="1.7") def to_hash_str(source, encoding="ascii"): # pragma: no cover -- deprecated & unused - "deprecated, use to_native_str() instead" + """deprecated, use to_native_str() instead""" return to_native_str(source, encoding, param="hash") #============================================================================= @@ -671,7 +672,7 @@ class Base64Engine(object): A string of 64 unique characters, which will be used to encode successive 6-bit chunks of data. A character's position within the string should correspond - to it's 6-bit value. + to its 6-bit value. :param big: Whether the encoding should be big-endian (default False). @@ -783,7 +784,7 @@ class Base64Engine(object): @property def charmap(self): - "charmap as unicode" + """charmap as unicode""" return self.bytemap.decode("latin-1") #=================================================================== @@ -811,7 +812,7 @@ class Base64Engine(object): return out def _encode_bytes_little(self, next_value, chunks, tail): - "helper used by encode_bytes() to handle little-endian encoding" + """helper used by encode_bytes() to handle little-endian encoding""" # # output bit layout: # @@ -850,7 +851,7 @@ class Base64Engine(object): yield v2>>4 def _encode_bytes_big(self, next_value, chunks, tail): - "helper used by encode_bytes() to handle big-endian encoding" + """helper used by encode_bytes() to handle big-endian encoding""" # # output bit layout: # @@ -916,7 +917,7 @@ class Base64Engine(object): raise ValueError("invalid character: %r" % (err.args[0],)) def _decode_bytes_little(self, next_value, chunks, tail): - "helper used by decode_bytes() to handle little-endian encoding" + """helper used by decode_bytes() to handle little-endian encoding""" # # input bit layout: # @@ -951,7 +952,7 @@ class Base64Engine(object): yield (v2>>2) | ((v3 & 0xF) << 4) def _decode_bytes_big(self, next_value, chunks, tail): - "helper used by decode_bytes() to handle big-endian encoding" + """helper used by decode_bytes() to handle big-endian encoding""" # # input bit layout: # @@ -993,21 +994,21 @@ class Base64Engine(object): # equivalent char with no padding bits set. def __make_padset(self, bits): - "helper to generate set of valid last chars & bytes" + """helper to generate set of valid last chars & bytes""" pset = set(c for i,c in enumerate(self.bytemap) if not i & bits) pset.update(c for i,c in enumerate(self.charmap) if not i & bits) return frozenset(pset) @memoized_property def _padinfo2(self): - "mask to clear padding bits, and valid last bytes (for strings 2 % 4)" + """mask to clear padding bits, and valid last bytes (for strings 2 % 4)""" # 4 bits of last char unused (lsb for big, msb for little) bits = 15 if self.big else (15<<2) return ~bits, self.__make_padset(bits) @memoized_property def _padinfo3(self): - "mask to clear padding bits, and valid last bytes (for strings 3 % 4)" + """mask to clear padding bits, and valid last bytes (for strings 3 % 4)""" # 2 bits of last char unused (lsb for big, msb for little) bits = 3 if self.big else (3<<4) return ~bits, self.__make_padset(bits) @@ -1072,14 +1073,14 @@ class Base64Engine(object): # transposed encoding/decoding #=================================================================== def encode_transposed_bytes(self, source, offsets): - "encode byte string, first transposing source using offset list" + """encode byte string, first transposing source using offset list""" if not isinstance(source, bytes): raise TypeError("source must be bytes, not %s" % (type(source),)) tmp = join_byte_elems(source[off] for off in offsets) return self.encode_bytes(tmp) def decode_transposed_bytes(self, source, offsets): - "decode byte string, then reverse transposition described by offset list" + """decode byte string, then reverse transposition described by offset list""" # NOTE: if transposition does not use all bytes of source, # the original can't be recovered... and join_byte_elems() will throw # an error because 1+ values in <buf> will be None. @@ -1133,7 +1134,7 @@ class Base64Engine(object): #--------------------------------------------------------------- def decode_int6(self, source): - "decode single character -> 6 bit integer" + """decode single character -> 6 bit integer""" if not isinstance(source, bytes): raise TypeError("source must be bytes, not %s" % (type(source),)) if len(source) != 1: @@ -1147,7 +1148,7 @@ class Base64Engine(object): raise ValueError("invalid character") def decode_int12(self, source): - "decodes 2 char string -> 12-bit integer" + """decodes 2 char string -> 12-bit integer""" if not isinstance(source, bytes): raise TypeError("source must be bytes, not %s" % (type(source),)) if len(source) != 2: @@ -1162,7 +1163,7 @@ class Base64Engine(object): raise ValueError("invalid character") def decode_int24(self, source): - "decodes 4 char string -> 24-bit integer" + """decodes 4 char string -> 24-bit integer""" if not isinstance(source, bytes): raise TypeError("source must be bytes, not %s" % (type(source),)) if len(source) != 4: @@ -1216,7 +1217,7 @@ class Base64Engine(object): #--------------------------------------------------------------- def encode_int6(self, value): - "encodes 6-bit integer -> single hash64 character" + """encodes 6-bit integer -> single hash64 character""" if value < 0 or value > 63: raise ValueError("value out of range") if PY3: @@ -1225,7 +1226,7 @@ class Base64Engine(object): return self._encode64(value) def encode_int12(self, value): - "encodes 12-bit integer -> 2 char string" + """encodes 12-bit integer -> 2 char string""" if value < 0 or value > 0xFFF: raise ValueError("value out of range") raw = [value & 0x3f, (value>>6) & 0x3f] @@ -1234,7 +1235,7 @@ class Base64Engine(object): return join_byte_elems(imap(self._encode64, raw)) def encode_int24(self, value): - "encodes 24-bit integer -> 4 char string" + """encodes 24-bit integer -> 4 char string""" if value < 0 or value > 0xFFFFFF: raise ValueError("value out of range") raw = [value & 0x3f, (value>>6) & 0x3f, @@ -1258,7 +1259,7 @@ class Base64Engine(object): #=================================================================== class LazyBase64Engine(Base64Engine): - "Base64Engine which delays initialization until it's accessed" + """Base64Engine which delays initialization until it's accessed""" _lazy_opts = None def __init__(self, *args, **kwds): @@ -1331,6 +1332,7 @@ def ab64_decode(data): try: from crypt import crypt as _crypt except ImportError: # pragma: no cover + _crypt = None has_crypt = False def safe_crypt(secret, hash): return None @@ -1429,6 +1431,13 @@ else: # On most other platforms the best timer is time.time() from time import time as tick +def parse_version(source): + """helper to parse version string""" + m = re.search(r"(\d+(?:\.\d+)+)", source) + if m: + return tuple(int(elem) for elem in m.group(1).split(".")) + return None + #============================================================================= # randomness #============================================================================= @@ -1451,13 +1460,13 @@ except NotImplementedError: # pragma: no cover has_urandom = False def genseed(value=None): - "generate prng seed value from system resources" + """generate prng seed value from system resources""" from hashlib import sha512 text = u("%s %s %s %s %.15f %.15f %s") % ( # if caller specified a seed value, mix it in value, - # if caller's seed value was an RNG, mix in bits from it's state + # if caller's seed value was an RNG, mix in bits from its state value.getrandbits(1<<15) if hasattr(value, "getrandbits") else None, # add current process id @@ -1574,7 +1583,7 @@ _handler_attrs = ( ) def is_crypt_handler(obj): - "check if object follows the :ref:`password-hash-api`" + """check if object follows the :ref:`password-hash-api`""" # XXX: change to use isinstance(obj, PasswordHash) under py26+? return all(hasattr(obj, name) for name in _handler_attrs) @@ -1585,7 +1594,7 @@ _context_attrs = ( ) def is_crypt_context(obj): - "check if object appears to be a :class:`~passlib.context.CryptContext` instance" + """check if object appears to be a :class:`~passlib.context.CryptContext` instance""" # XXX: change to use isinstance(obj, CryptContext)? return all(hasattr(obj, name) for name in _context_attrs) @@ -1595,12 +1604,12 @@ def is_crypt_context(obj): ## return hasattr(handler, "set_backend") def has_rounds_info(handler): - "check if handler provides the optional :ref:`rounds information <rounds-attributes>` attributes" + """check if handler provides the optional :ref:`rounds information <rounds-attributes>` attributes""" return ('rounds' in handler.setting_kwds and getattr(handler, "min_rounds", None) is not None) def has_salt_info(handler): - "check if handler provides the optional :ref:`salt information <salt-attributes>` attributes" + """check if handler provides the optional :ref:`salt information <salt-attributes>` attributes""" return ('salt' in handler.setting_kwds and getattr(handler, "min_salt_size", None) is not None) |
