Skip to content

Commit

Permalink
Refactoring to avoid builtin shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Shafer committed May 22, 2018
1 parent e85b6d7 commit b5eacb7
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions puresasl/mechanisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def dispose(self):
# functions used in DigestMD5 which were originally defined in the now-removed
# util module

def bytes(text):
def to_bytes(text):
"""
Convert Unicode text to UTF-8 encoded bytes.
Expand Down Expand Up @@ -252,7 +252,7 @@ def quote(text):
:param text: A Unicode or byte string.
"""
text = bytes(text)
text = to_bytes(text)
return b'"' + text.replace(b'\\', b'\\\\').replace(b'"', b'\\"') + b'"'


Expand Down Expand Up @@ -301,16 +301,16 @@ def response(self):
if getattr(self, 'realm', None) is not None:
resp['realm'] = quote(self.realm)

resp['username'] = quote(bytes(self.username))
resp['username'] = quote(to_bytes(self.username))
resp['nonce'] = quote(self.nonce)
if self.nc == 0:
self.cnonce = bytes('%s' % random.random())[2:]
self.cnonce = to_bytes('%s' % random.random())[2:]
resp['cnonce'] = quote(self.cnonce)
self.nc += 1
resp['nc'] = bytes('%08x' % self.nc)
resp['nc'] = to_bytes('%08x' % self.nc)

self._digest_uri = (
bytes(self.sasl.service) + b'/' + bytes(self.sasl.host))
to_bytes(self.sasl.service) + b'/' + to_bytes(self.sasl.host))
resp['digest-uri'] = quote(self._digest_uri)

a2 = b'AUTHENTICATE:' + self._digest_uri
Expand All @@ -320,7 +320,7 @@ def response(self):
resp['response'] = self.gen_hash(a2)
return b','.join(
[
bytes(k) + b'=' + bytes(v)
to_bytes(k) + b'=' + to_bytes(v)
for k, v in resp.items()
]
)
Expand All @@ -343,7 +343,7 @@ def parse_challenge(challenge):
escaped = False
for c in challenge:
if sys.version_info[0] == 3:
c = bytes([c])
c = to_bytes([c])
if in_var:
if c.isspace():
continue
Expand Down Expand Up @@ -386,9 +386,9 @@ def parse_challenge(challenge):
def gen_hash(self, a2):
if not getattr(self, 'key_hash', None):
key_hash = hashlib.md5()
user = bytes(self.username)
password = bytes(self.password)
realm = bytes(self.realm)
user = to_bytes(self.username)
password = to_bytes(self.password)
realm = to_bytes(self.realm)
kh = user + b':' + realm + b':' + password
key_hash.update(kh)
self.key_hash = key_hash.digest()
Expand All @@ -398,14 +398,14 @@ def gen_hash(self, a2):
a1.update(a1h)
response = hashlib.md5()
self._a1 = a1.digest()
rv = bytes(a1.hexdigest().lower())
rv = to_bytes(a1.hexdigest().lower())
rv += b':' + self.nonce
rv += b':' + bytes('%08x' % self.nc)
rv += b':' + to_bytes('%08x' % self.nc)
rv += b':' + self.cnonce
rv += b':' + self.qop
rv += b':' + bytes(hashlib.md5(a2).hexdigest().lower())
rv += b':' + to_bytes(hashlib.md5(a2).hexdigest().lower())
response.update(rv)
return bytes(response.hexdigest().lower())
return to_bytes(response.hexdigest().lower())

def authenticate_server(self, cmp_hash):
a2 = b':' + self._digest_uri
Expand Down

0 comments on commit b5eacb7

Please sign in to comment.