Use strings not unicode in password module
authorJeremy Stanley <fungi@yuggoth.org>
Wed, 25 Jan 2012 00:20:04 +0000 (00:20 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Wed, 25 Jan 2012 00:20:04 +0000 (00:20 +0000)
* lib/mudpy/password.py: Python 3000 treats strings as unicode by default,
so we should too.

lib/mudpy/password.py

index 94d690b..20615b9 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
-u"""Password hashing functions and constants for the mudpy engine."""
+"""Password hashing functions and constants for the mudpy engine."""
 
 
-# Copyright (c) 2004-2011 Jeremy Stanley <fungi@yuggoth.org>. Permission
+# Copyright (c) 2004-2012 Jeremy Stanley <fungi@yuggoth.org>. Permission
 # to use, copy, modify, and distribute this software is granted under
 # terms provided in the LICENSE file distributed with this software.
 
 # to use, copy, modify, and distribute this software is granted under
 # terms provided in the LICENSE file distributed with this software.
 
@@ -39,8 +39,8 @@ def _bytes_to_text(byte_sequence):
     import base64
     return base64.b64encode(
         byte_sequence,
     import base64
     return base64.b64encode(
         byte_sequence,
-        u"./".encode(u"ascii")
-    ).rstrip(u"=")
+        "./".encode("ascii")
+    ).rstrip("=")
 
 
 def _generate_salt(salt_len=2):
 
 
 def _generate_salt(salt_len=2):
@@ -60,7 +60,7 @@ def _generate_salt(salt_len=2):
     return _bytes_to_text(_pack_bytes(salt))[:salt_len]
 
 
     return _bytes_to_text(_pack_bytes(salt))[:salt_len]
 
 
-def upgrade_legacy_hash(legacy_hash, salt, sep=u"$"):
+def upgrade_legacy_hash(legacy_hash, salt, sep="$"):
     """
     This utility function is meant to provide a migration path for users
     of mudpy's legacy account-name-salted MD5 hexdigest password hashes.
     """
     This utility function is meant to provide a migration path for users
     of mudpy's legacy account-name-salted MD5 hexdigest password hashes.
@@ -69,14 +69,14 @@ def upgrade_legacy_hash(legacy_hash, salt, sep=u"$"):
     returned.
     """
     import re
     returned.
     """
     import re
-    assert re.match(u"^[0-9a-f]{32}$",
+    assert re.match("^[0-9a-f]{32}$",
                     legacy_hash), "Not a valid MD5 hexdigest"
     # this needs to be declared as b"" in 2to3
     collapsed = ""
     for i in xrange(16):
         # this needs to become a byte() call in 2to3
         collapsed += chr(int(legacy_hash[2 * i:2 * i + 2], 16))
                     legacy_hash), "Not a valid MD5 hexdigest"
     # this needs to be declared as b"" in 2to3
     collapsed = ""
     for i in xrange(16):
         # this needs to become a byte() call in 2to3
         collapsed += chr(int(legacy_hash[2 * i:2 * i + 2], 16))
-    return u"%s%s%s%s%s%s%s%s" % (
+    return "%s%s%s%s%s%s%s%s" % (
         sep,
         MD5,
         sep,
         sep,
         MD5,
         sep,
@@ -94,7 +94,7 @@ def create(
     algorithm=SHA1,
     rounds=4,
     salt_len=2,
     algorithm=SHA1,
     rounds=4,
     salt_len=2,
-    sep=u"$"
+    sep="$"
 ):
     """
     The meat of the module, this function takes a provided password and
 ):
     """
     The meat of the module, this function takes a provided password and
@@ -167,7 +167,7 @@ def create(
         hashed = algorithms[algorithm](hashed).digest()
 
     # concatenate the output fields, coercing into text form as needed
         hashed = algorithms[algorithm](hashed).digest()
 
     # concatenate the output fields, coercing into text form as needed
-    return u"%s%s%s%s%s%s%s%s" % (
+    return "%s%s%s%s%s%s%s%s" % (
         sep, algorithm, sep, rounds, sep, salt, sep, _bytes_to_text(hashed)
     )
 
         sep, algorithm, sep, rounds, sep, salt, sep, _bytes_to_text(hashed)
     )
 
@@ -180,7 +180,8 @@ def verify(password, encoded_hash):
     comes out the same as the encoded_hash.
     """
     sep = encoded_hash[0]
     comes out the same as the encoded_hash.
     """
     sep = encoded_hash[0]
-    algorithm, rounds, salt, hashed = encoded_hash[1:].split(sep)
+    import mudpy.misc
+    algorithm, rounds, salt, hashed = encoded_hash.split(sep)[1:]
     if encoded_hash == create(
        password=password,
        salt=salt,
     if encoded_hash == create(
        password=password,
        salt=salt,