python/samba: Ensure md5 always provided with bytes
authorNoel Power <noel.power@suse.com>
Fri, 4 May 2018 11:05:27 +0000 (12:05 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 12 May 2018 19:38:16 +0000 (21:38 +0200)
To allow code run in both python3 and python2 we have to ensure
that md5 always receives bytes

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/visualize.py
python/samba/tests/password_hash.py
python/samba/tests/samba_tool/user_wdigest.py

index 6f880ae61e82beb39654ddb30596d65d225be34d..576f48d41ef9208bfe9cdab1561383fa7b70f8ab 100644 (file)
@@ -34,6 +34,7 @@ from ldb import SCOPE_BASE, SCOPE_SUBTREE, LdbError
 import time
 from samba.kcc import KCC
 from samba.kcc.kcc_utils import KCCError
+from samba.compat import text_type
 
 COMMON_OPTIONS = [
     Option("-H", "--URL", help="LDB URL for database or target server",
@@ -161,7 +162,10 @@ def colour_hash(x):
     """Generate a randomish but consistent darkish colour based on the
     given object."""
     from hashlib import md5
-    c = int(md5(str(x)).hexdigest()[:6], base=16) & 0x7f7f7f
+    tmp_str = str(x)
+    if isinstance(tmp_str, text_type):
+        tmp_str = tmp_str.encode('utf8')
+    c = int(md5(tmp_str).hexdigest()[:6], base=16) & 0x7f7f7f
     return '#%06x' % c
 
 
index 4c4dbcf2fd749ff6a7595de85a3a574de835a042..745ac704c9fb21fcb527de8ded5fc2de697d1d58 100644 (file)
@@ -35,6 +35,7 @@ import samba
 import binascii
 from hashlib import md5
 import crypt
+from samba.compat import text_type
 
 
 USER_NAME = "PasswordHashTestUser"
@@ -61,6 +62,9 @@ def get_package(sc, name):
 def calc_digest(user, realm, password):
 
     data = "%s:%s:%s" % (user, realm, password)
+    if isinstance(data, text_type):
+        data = data.encode('utf8')
+
     return md5(data).hexdigest()
 
 
index 35283ebfcb320432aa66de9209c209e387aaf33d..eddb79f4d184186dd0dbd74645db10faa45a72a4 100644 (file)
@@ -33,6 +33,7 @@ from samba.dcerpc import drsblobs
 from hashlib import md5
 import random
 import string
+from samba.compat import text_type
 
 USER_NAME = "WdigestTestUser"
 # Create a random 32 character password, containing only letters and
@@ -45,6 +46,9 @@ USER_PASS = ''.join(random.choice(string.ascii_uppercase +
 #
 def calc_digest(user, realm, password):
     data = "%s:%s:%s" % (user, realm, password)
+    if isinstance(data, text_type):
+        data = data.encode('utf8')
+
     return "%s:%s:%s" % (user, realm, md5(data).hexdigest())