diff options
author | Eli Collins <elic@assurancetechnologies.com> | 2020-10-06 15:25:16 -0400 |
---|---|---|
committer | Eli Collins <elic@assurancetechnologies.com> | 2020-10-06 15:25:16 -0400 |
commit | 240ffe471e48f9d36de936389f88b69f10166032 (patch) | |
tree | 86e0bd20766d3c4fc668d0a95c75468ac2b5262f | |
parent | 29f2d794b93797e38ef30e2750926527d177c413 (diff) | |
download | passlib-240ffe471e48f9d36de936389f88b69f10166032.tar.gz |
cleanup old python compat -- removed join_byte_elems() and join_byte_values() wrappers
-rw-r--r-- | passlib/crypto/des.py | 7 | ||||
-rw-r--r-- | passlib/crypto/digest.py | 7 | ||||
-rw-r--r-- | passlib/handlers/cisco.py | 5 | ||||
-rw-r--r-- | passlib/utils/__init__.py | 8 | ||||
-rw-r--r-- | passlib/utils/binary.py | 20 | ||||
-rw-r--r-- | passlib/utils/compat/__init__.py | 9 | ||||
-rw-r--r-- | passlib/utils/handlers.py | 4 |
7 files changed, 23 insertions, 37 deletions
diff --git a/passlib/crypto/des.py b/passlib/crypto/des.py index 8f9d2f8..b473846 100644 --- a/passlib/crypto/des.py +++ b/passlib/crypto/des.py @@ -47,7 +47,7 @@ The netbsd des-crypt implementation has some nice notes on how this all works - import struct # pkg from passlib import exc -from passlib.utils.compat import join_byte_values, int_types +from passlib.utils.compat import int_types # local __all__ = [ "expand_des_key", @@ -623,9 +623,8 @@ def expand_des_key(key): # but the parity bit would just be ignored in des_encrypt_block(), # so not bothering to use it. # XXX: could make parity-restoring optionally available via flag - ##return join_byte_values(expand_7bit((key >> shift) & 0x7f) - ## for shift in _EXPAND_ITER) - return join_byte_values(((key>>shift) & 0x7f)<<1 for shift in _EXPAND_ITER) + ##return bytes(expand_7bit((key >> shift) & 0x7f) for shift in _EXPAND_ITER) + return bytes(((key >> shift) & 0x7f) << 1 for shift in _EXPAND_ITER) def shrink_des_key(key): """convert DES key from 8 bytes to 7 bytes (by discarding the parity bits)""" diff --git a/passlib/crypto/digest.py b/passlib/crypto/digest.py index e46ff15..4d2b983 100644 --- a/passlib/crypto/digest.py +++ b/passlib/crypto/digest.py @@ -30,8 +30,7 @@ except ImportError: _fast_pbkdf2_hmac = None # pkg from passlib import exc -from passlib.utils import join_bytes, to_native_str, join_byte_values, to_bytes, \ - SequenceMixin, as_bool +from passlib.utils import join_bytes, to_native_str, to_bytes, SequenceMixin, as_bool from passlib.utils.compat import int_types, unicode_or_bytes from passlib.utils.decor import memoized_property # local @@ -616,8 +615,8 @@ if as_bool(os.environ.get("PASSLIB_MOCK_FIPS_MODE")): #============================================================================= #: translation tables used by compile_hmac() -_TRANS_5C = join_byte_values((x ^ 0x5C) for x in range(256)) -_TRANS_36 = join_byte_values((x ^ 0x36) for x in range(256)) +_TRANS_5C = bytes((x ^ 0x5C) for x in range(256)) +_TRANS_36 = bytes((x ^ 0x36) for x in range(256)) def compile_hmac(digest, key, multipart=False): """ diff --git a/passlib/handlers/cisco.py b/passlib/handlers/cisco.py index 01c52bf..09141c8 100644 --- a/passlib/handlers/cisco.py +++ b/passlib/handlers/cisco.py @@ -13,7 +13,6 @@ from warnings import warn # pkg from passlib.utils import right_pad_string, to_unicode, repeat_string, to_bytes from passlib.utils.binary import h64 -from passlib.utils.compat import join_byte_values, join_byte_elems import passlib.utils.handlers as uh # local __all__ = [ @@ -218,7 +217,7 @@ class cisco_pix(uh.HasUserContext, uh.StaticHandler): # 16 bytes, which may have been a general 'char password[]' # size limit under PIX # - digest = join_byte_elems(c for i, c in enumerate(digest) if (i + 1) & 3) + digest = bytes(c for i, c in enumerate(digest) if (i + 1) & 3) # # encode using Hash64 @@ -429,7 +428,7 @@ class cisco_type7(uh.GenericHandler): """xor static key against data - encrypts & decrypts""" key = cls._key key_size = len(key) - return join_byte_values( + return bytes( value ^ ord(key[(salt + idx) % key_size]) for idx, value in enumerate(data) ) diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index aede331..94833ad 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -61,8 +61,7 @@ from passlib.utils.decor import ( hybrid_method, ) from passlib.exc import ExpectedStringError, ExpectedTypeError -from passlib.utils.compat import (add_doc, join_bytes, join_byte_values, - join_byte_elems, +from passlib.utils.compat import (add_doc, join_bytes, join_unicode, unicode_or_bytes, get_method_function, PYPY) @@ -1059,7 +1058,8 @@ def getrandbytes(rng, count): yield value & 0xff value >>= 3 i += 1 - return join_byte_values(helper()) + return bytes(helper()) + def getrandstr(rng, charset, count): """return string containing *count* number of chars/bytes, whose elements are drawn from specified charset, using specified rng""" @@ -1088,7 +1088,7 @@ def getrandstr(rng, charset, count): if isinstance(charset, str): return join_unicode(helper()) else: - return join_byte_elems(helper()) + return bytes(helper()) _52charset = '2346789ABCDEFGHJKMNPQRTUVWXYZabcdefghjkmnpqrstuvwxyz' diff --git a/passlib/utils/binary.py b/passlib/utils/binary.py index 37bfefa..745366f 100644 --- a/passlib/utils/binary.py +++ b/passlib/utils/binary.py @@ -19,7 +19,7 @@ log = logging.getLogger(__name__) from passlib import exc from passlib.utils.compat import ( bascii_to_str, - iter_byte_chars, join_byte_values, join_byte_elems, + iter_byte_chars, unicode_or_bytes, ) from passlib.utils.decor import memoized_property @@ -91,7 +91,7 @@ LOWER_HEX_CHARS = u"0123456789abcdef" #: special byte string containing all possible byte values #: NOTE: for efficiency, this is treated as singleton by some of the code -ALL_BYTE_VALUES = join_byte_values(range(256)) +ALL_BYTE_VALUES = bytes(range(256)) #: some string constants we reuse B_EMPTY = b'' @@ -385,7 +385,7 @@ class Base64Engine(object): chunks, tail = divmod(len(source), 3) next_value = iter(source).__next__ gen = self._encode_bytes(next_value, chunks, tail) - out = join_byte_elems(map(self._encode64, gen)) + out = bytes(map(self._encode64, gen)) ##if tail: ## padding = self.padding ## if padding: @@ -492,7 +492,7 @@ class Base64Engine(object): raise ValueError("input string length cannot be == 1 mod 4") next_value = map(self._decode64, source).__next__ try: - return join_byte_values(self._decode_bytes(next_value, chunks, tail)) + return bytes(self._decode_bytes(next_value, chunks, tail)) except KeyError as err: raise ValueError("invalid character: %r" % (err.args[0],)) @@ -655,19 +655,19 @@ class Base64Engine(object): """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) + tmp = bytes(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""" # NOTE: if transposition does not use all bytes of source, - # the original can't be recovered... and join_byte_elems() will throw + # the original can't be recovered... and bytes() will throw # an error because 1+ values in <buf> will be None. tmp = self.decode_bytes(source) buf = [None] * len(offsets) for off, char in zip(offsets, tmp): buf[off] = char - return join_byte_elems(buf) + return bytes(buf) #=================================================================== # integer decoding helpers - mainly used by des_crypt family @@ -791,7 +791,7 @@ class Base64Engine(object): else: itr = range(0, bits, 6) # padding is msb, so no change needed. - return join_byte_elems(map(self._encode64, + return bytes(map(self._encode64, ((value>>off) & 0x3f for off in itr))) #--------------------------------------------------------------- @@ -811,7 +811,7 @@ class Base64Engine(object): raw = [value & 0x3f, (value>>6) & 0x3f] if self.big: raw = reversed(raw) - return join_byte_elems(map(self._encode64, raw)) + return bytes(map(self._encode64, raw)) def encode_int24(self, value): """encodes 24-bit integer -> 4 char string""" @@ -821,7 +821,7 @@ class Base64Engine(object): (value>>12) & 0x3f, (value>>18) & 0x3f] if self.big: raw = reversed(raw) - return join_byte_elems(map(self._encode64, raw)) + return bytes(map(self._encode64, raw)) def encode_int30(self, value): """decode 5 char string -> 30 bit integer""" diff --git a/passlib/utils/compat/__init__.py b/passlib/utils/compat/__init__.py index f4ea2a6..f90d914 100644 --- a/passlib/utils/compat/__init__.py +++ b/passlib/utils/compat/__init__.py @@ -45,7 +45,6 @@ __all__ = [ 'bascii_to_str', 'str_to_bascii', 'join_unicode', 'join_bytes', - 'join_byte_values', 'join_byte_elems', # context helpers 'nullcontext', @@ -84,8 +83,6 @@ if True: # legacy PY3 indent assert isinstance(s, str) return s.encode("ascii") - join_byte_values = join_byte_elems = bytes - def iter_byte_chars(s): assert isinstance(s, bytes) # FIXME: there has to be a better way to do this @@ -95,12 +92,6 @@ if True: # legacy PY3 indent add_doc(bascii_to_str, "helper to convert ascii bytes -> native str") add_doc(str_to_bascii, "helper to convert ascii native str -> bytes") -# join_byte_values -- function to convert list of ordinal integers to byte string. - -# join_byte_elems -- function to convert list of byte elements to byte string; -# i.e. what's returned by ``b('a')[0]``... -# this is 97 under PY3. - # byte_elem_value -- function to convert byte element to integer -- a noop under PY3 add_doc(iter_byte_chars, "iterate over byte string as sequence of 1-byte strings") diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py index d16b7e5..8c02e69 100644 --- a/passlib/utils/handlers.py +++ b/passlib/utils/handlers.py @@ -26,9 +26,7 @@ from passlib.utils.binary import ( HEX_CHARS, UPPER_HEX_CHARS, LOWER_HEX_CHARS, ALL_BYTE_VALUES, ) -from passlib.utils.compat import join_byte_values, \ - join_unicode, \ - join_unicode, unicode_or_bytes, int_types +from passlib.utils.compat import join_unicode, unicode_or_bytes, int_types from passlib.utils.decor import classproperty, deprecated_method # local __all__ = [ |