diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2011-12-28 17:15:31 -0500 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2011-12-28 17:15:31 -0500 |
| commit | 9d1ca56acb757a034d66eb0b97acf0ca06245146 (patch) | |
| tree | 4a08b829d3a70d0430aa5dfbb03cac96238dcec5 /passlib/utils | |
| parent | dce7529fa18ab75cc42df175abee660836326ea5 (diff) | |
| download | passlib-9d1ca56acb757a034d66eb0b97acf0ca06245146.tar.gz | |
deprecated to_hash_str, replaced all instances with to_native_str
decided that to_hash_str will always return native string,
feature of hashes being returned as unicode under python 2
is better done through a CryptContext option.
Diffstat (limited to 'passlib/utils')
| -rw-r--r-- | passlib/utils/__init__.py | 40 | ||||
| -rw-r--r-- | passlib/utils/handlers.py | 8 | ||||
| -rw-r--r-- | passlib/utils/md4.py | 5 | ||||
| -rw-r--r-- | passlib/utils/pbkdf2.py | 6 |
4 files changed, 28 insertions, 31 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index 5e182f6..101afd3 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -29,7 +29,7 @@ __all__ = [ ## "abstractclassmethod", #byte compat aliases - 'bytes', 'native_str', + 'bytes', #misc 'os_crypt', @@ -41,7 +41,6 @@ __all__ = [ #bytes<->unicode 'to_bytes', 'to_unicode', - 'to_native_str', 'is_same_codec', # string manipulation @@ -136,14 +135,6 @@ class PasslibPolicyWarning(UserWarning): but the warning is issued as a sign the configuration may need updating. """ -#========================================================== -#bytes compat aliases - bytes, native_str, b() -#========================================================== - -# NOTE: most of this has been moved to compat() - -native_str = str - #================================================================================= #os crypt helpers #================================================================================= @@ -390,7 +381,6 @@ def to_unicode(source, source_encoding="utf-8", errname="value"): if PY3: def to_native_str(source, encoding="utf-8", errname="value"): - assert encoding if isinstance(source, bytes): return source.decode(encoding) elif isinstance(source, unicode): @@ -400,7 +390,6 @@ if PY3: (errname, type(source))) else: def to_native_str(source, encoding="utf-8", errname="value"): - assert encoding if isinstance(source, bytes): return source elif isinstance(source, unicode): @@ -409,25 +398,32 @@ else: raise TypeError("%s must be unicode or bytes, not %s" % (errname, type(source))) -_add_doc(to_native_str, """take in unicode or bytes, return native string +_add_doc(to_native_str, + """take in unicode or bytes, return native string python 2: encodes unicode using specified encoding, leaves bytes alone. python 3: decodes bytes using specified encoding, leaves unicode alone. :raises TypeError: if source is not unicode or bytes. - :arg source: source bytes/unicode to process - :arg encoding: encoding to use when encoding unicode / decoding bytes - :param errname: optional name of variable/noun to reference when raising errors + :arg source: + source unicode or bytes string. + + :arg encoding: + encoding to use when encoding unicode or decoding bytes. + this defaults to ``"utf-8"``. + + :param errname: + optional name of variable/noun to reference when raising errors. :returns: :class:`str` instance """) -def to_hash_str(hash, encoding="ascii", errname="hash"): - "given hash string as bytes or unicode; normalize according to hash policy" - #NOTE: for now, policy is ascii-bytes under py2, unicode under py3. - # but plan to make flag allowing apps to enable unicode behavior under py2. - return to_native_str(hash, encoding, errname) +def to_hash_str(source, encoding="ascii"): + "deprecated, use to_native_str() instead" + warn("to_hash_str() is deprecated, and will be removed in passlib 1.7", + DeprecationWarning, stacklevel=2) + return to_native_str(source, encoding, 'hash') #-------------------------------------------------- #support utils @@ -521,7 +517,7 @@ def splitcomma(source, sep=","): .. deprecated:: 1.6, will be removed in 1.7 """ - warn("splitcomma() is deprecated, will be removed in passlib 1.7", + warn("splitcomma() is deprecated, and will be removed in passlib 1.7", DeprecationWarning, stacklevel=2) return [ elem.strip() diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py index 98c61fa..53bd476 100644 --- a/passlib/utils/handlers.py +++ b/passlib/utils/handlers.py @@ -14,7 +14,7 @@ from warnings import warn #site #libs from passlib.registry import get_crypt_handler -from passlib.utils import to_hash_str, bytes, b, consteq, \ +from passlib.utils import to_native_str, bytes, b, consteq, \ classproperty, h64, getrandstr, getrandbytes, \ rng, is_crypt_handler, ALL_BYTE_VALUES, MissingBackendError from passlib.utils.compat import unicode, u @@ -126,7 +126,7 @@ def render_mc2(ident, salt, checksum, sep=u("$")): hash = u("%s%s%s%s") % (ident, salt, sep, checksum) else: hash = u("%s%s") % (ident, salt) - return to_hash_str(hash) + return to_native_str(hash) def render_mc3(ident, rounds, salt, checksum, sep=u("$")): "format hash using 3-part modular crypt format; inverse of parse_mc3" @@ -134,7 +134,7 @@ def render_mc3(ident, rounds, salt, checksum, sep=u("$")): hash = u("%s%s%s%s%s%s") % (ident, rounds, sep, salt, sep, checksum) else: hash = u("%s%s%s%s") % (ident, rounds, sep, salt) - return to_hash_str(hash) + return to_native_str(hash) #===================================================== #StaticHandler @@ -1290,7 +1290,7 @@ class PrefixWrapper(object): if not hash.startswith(orig_prefix): raise ValueError("not a valid %s hash" % (self.wrapped.name,)) wrapped = self.prefix + hash[len(orig_prefix):] - return to_hash_str(wrapped) + return to_native_str(wrapped) def identify(self, hash): if not hash: diff --git a/passlib/utils/md4.py b/passlib/utils/md4.py index 4935999..0ee3324 100644 --- a/passlib/utils/md4.py +++ b/passlib/utils/md4.py @@ -16,7 +16,7 @@ import struct from warnings import warn #site from passlib.utils import b, bytes, to_native_str -from passlib.utils.compat import irange +from passlib.utils.compat import irange, PY3 #local __all__ = [ "md4" ] #========================================================================= @@ -224,7 +224,8 @@ class md4(object): return out def hexdigest(self): - return to_native_str(hexlify(self.digest()), "latin-1") + # PY3: hexlify returns bytes, but hexdigest should return str. + return to_native_str(hexlify(self.digest())) #========================================================================= #eoc diff --git a/passlib/utils/pbkdf2.py b/passlib/utils/pbkdf2.py index aa35327..27ccf4c 100644 --- a/passlib/utils/pbkdf2.py +++ b/passlib/utils/pbkdf2.py @@ -20,7 +20,7 @@ try: except ImportError: _EVP = None #pkg -from passlib.utils import xor_bytes, to_bytes, native_str, b, bytes +from passlib.utils import xor_bytes, to_bytes, b, bytes from passlib.utils.compat import irange, callable, int_types from passlib.utils.compat.aliases import BytesIO #local @@ -133,7 +133,7 @@ def get_prf(name): global _prf_cache if name in _prf_cache: return _prf_cache[name] - if isinstance(name, native_str): + if isinstance(name, str): if name.startswith("hmac-") or name.startswith("hmac_"): retval = _get_hmac_prf(name[5:]) else: @@ -191,7 +191,7 @@ def pbkdf1(secret, salt, rounds, keylen, hash="sha1"): raise ValueError("keylen must be at least 0") #resolve hash - if isinstance(hash, native_str): + if isinstance(hash, str): #check for builtin hash hf = getattr(hashlib, hash, None) if hf is None: |
