summaryrefslogtreecommitdiff
path: root/passlib/handlers/sun_md5_crypt.py
diff options
context:
space:
mode:
Diffstat (limited to 'passlib/handlers/sun_md5_crypt.py')
-rw-r--r--passlib/handlers/sun_md5_crypt.py39
1 files changed, 19 insertions, 20 deletions
diff --git a/passlib/handlers/sun_md5_crypt.py b/passlib/handlers/sun_md5_crypt.py
index 0eeb4e7..d80d7ff 100644
--- a/passlib/handlers/sun_md5_crypt.py
+++ b/passlib/handlers/sun_md5_crypt.py
@@ -19,8 +19,7 @@ from warnings import warn
# pkg
from passlib.utils import to_unicode
from passlib.utils.binary import h64
-from passlib.utils.compat import byte_elem_value, irange, u, \
- uascii_to_str, unicode, str_to_bascii
+from passlib.utils.compat import str_to_bascii
import passlib.utils.handlers as uh
# local
__all__ = [
@@ -73,7 +72,7 @@ MAGIC_HAMLET = (
)
# NOTE: these sequences are pre-calculated iteration ranges used by X & Y loops w/in rounds function below
-xr = irange(7)
+xr = range(7)
_XY_ROUNDS = [
tuple((i,i,i+3) for i in xr), # xrounds 0
tuple((i,i+1,i+4) for i in xr), # xrounds 1
@@ -124,7 +123,7 @@ def raw_sun_md5_crypt(secret, rounds, salt):
round = 0
while round < real_rounds:
# convert last result byte string to list of byte-ints for easy access
- rval = [ byte_elem_value(c) for c in result ].__getitem__
+ rval = [c for c in result].__getitem__
# build up X bit by bit
x = 0
@@ -151,7 +150,7 @@ def raw_sun_md5_crypt(secret, rounds, salt):
h = md5(result)
if coin:
h.update(MAGIC_HAMLET)
- h.update(unicode(round).encode("ascii"))
+ h.update(str(round).encode("ascii"))
result = h.digest()
round += 1
@@ -237,7 +236,7 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
# XXX: ^ not sure what it does if past this bound... does 32 int roll over?
rounds_cost = "linear"
- ident_values = (u("$md5$"), u("$md5,"))
+ ident_values = (u"$md5$", u"$md5,")
#===================================================================
# instance attrs
@@ -249,7 +248,7 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
#===================================================================
def __init__(self, bare_salt=False, **kwds):
self.bare_salt = bare_salt
- super(sun_md5_crypt, self).__init__(**kwds)
+ super().__init__(**kwds)
#===================================================================
# internal helpers
@@ -268,11 +267,11 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
# if so, parse and validate it.
# by end, set 'rounds' to int value, and 'tail' containing salt+chk
#
- if hash.startswith(u("$md5$")):
+ if hash.startswith(u"$md5$"):
rounds = 0
salt_idx = 5
- elif hash.startswith(u("$md5,rounds=")):
- idx = hash.find(u("$"), 12)
+ elif hash.startswith(u"$md5,rounds="):
+ idx = hash.find(u"$", 12)
if idx == -1:
raise uh.exc.MalformedHashError(cls, "unexpected end of rounds")
rstr = hash[12:idx]
@@ -280,7 +279,7 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
rounds = int(rstr)
except ValueError:
raise uh.exc.MalformedHashError(cls, "bad rounds")
- if rstr != unicode(rounds):
+ if rstr != str(rounds):
raise uh.exc.ZeroPaddedRoundsError(cls)
if rounds == 0:
# NOTE: not sure if this is forbidden by spec or not;
@@ -296,20 +295,20 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
# to deal cleanly with some backward-compatible workarounds
# implemented by original implementation.
#
- chk_idx = hash.rfind(u("$"), salt_idx)
+ chk_idx = hash.rfind(u"$", salt_idx)
if chk_idx == -1:
# ''-config for $-hash
salt = hash[salt_idx:]
chk = None
bare_salt = True
elif chk_idx == len(hash)-1:
- if chk_idx > salt_idx and hash[-2] == u("$"):
+ if chk_idx > salt_idx and hash[-2] == u"$":
raise uh.exc.MalformedHashError(cls, "too many '$' separators")
# $-config for $$-hash
salt = hash[salt_idx:-1]
chk = None
bare_salt = False
- elif chk_idx > 0 and hash[chk_idx-1] == u("$"):
+ elif chk_idx > 0 and hash[chk_idx-1] == u"$":
# $$-hash
salt = hash[salt_idx:chk_idx-1]
chk = hash[chk_idx+1:]
@@ -328,16 +327,16 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
)
def to_string(self, _withchk=True):
- ss = u('') if self.bare_salt else u('$')
+ ss = u'' if self.bare_salt else u'$'
rounds = self.rounds
if rounds > 0:
- hash = u("$md5,rounds=%d$%s%s") % (rounds, self.salt, ss)
+ hash = u"$md5,rounds=%d$%s%s" % (rounds, self.salt, ss)
else:
- hash = u("$md5$%s%s") % (self.salt, ss)
+ hash = u"$md5$%s%s" % (self.salt, ss)
if _withchk:
chk = self.checksum
- hash = u("%s$%s") % (hash, chk)
- return uascii_to_str(hash)
+ hash = u"%s$%s" % (hash, chk)
+ return hash
#===================================================================
# primary interface
@@ -349,7 +348,7 @@ class sun_md5_crypt(uh.HasRounds, uh.HasSalt, uh.GenericHandler):
def _calc_checksum(self, secret):
# NOTE: no reference for how sun_md5_crypt handles unicode
- if isinstance(secret, unicode):
+ if isinstance(secret, str):
secret = secret.encode("utf-8")
config = str_to_bascii(self.to_string(_withchk=False))
return raw_sun_md5_crypt(secret, self.rounds, config).decode("ascii")