md5crypt.py
branchtls-sprint
changeset 1132 96752791c2b6
parent 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
1131:544609e83317 1132:96752791c2b6
    51         v = v >> 6
    51         v = v >> 6
    52     return ret
    52     return ret
    53 
    53 
    54 
    54 
    55 def crypt(pw, salt, magic=None):
    55 def crypt(pw, salt, magic=None):
    56     if magic==None:
    56     if magic is None:
    57         magic = MAGIC
    57         magic = MAGIC
    58     # Take care of the magic string if present
    58     # Take care of the magic string if present
    59     if salt[:len(magic)] == magic:
    59     if salt[:len(magic)] == magic:
    60         salt = salt[len(magic):]
    60         salt = salt[len(magic):]
    61     # salt can have up to 8 characters:
    61     # salt can have up to 8 characters:
    62     salt = salt.split('$', 1)[0]
    62     salt = salt.split('$', 1)[0]
    63     salt = salt[:8]
    63     salt = salt[:8]
    64     ctx = pw + magic + salt
    64     ctx = pw + magic + salt
    65     final = md5.md5(pw + salt + pw).digest()
    65     final = md5.md5(pw + salt + pw).digest()
    66     for pl in range(len(pw),0,-16):
    66     for pl in xrange(len(pw), 0, -16):
    67         if pl > 16:
    67         if pl > 16:
    68             ctx = ctx + final[:16]
    68             ctx = ctx + final[:16]
    69         else:
    69         else:
    70             ctx = ctx + final[:pl]
    70             ctx = ctx + final[:pl]
    71     # Now the 'weird' xform (??)
    71     # Now the 'weird' xform (??)
    78         i = i >> 1
    78         i = i >> 1
    79     final = md5.md5(ctx).digest()
    79     final = md5.md5(ctx).digest()
    80     # The following is supposed to make
    80     # The following is supposed to make
    81     # things run slower. 
    81     # things run slower. 
    82     # my question: WTF???
    82     # my question: WTF???
    83     for i in range(1000):
    83     for i in xrange(1000):
    84         ctx1 = ''
    84         ctx1 = ''
    85         if i & 1:
    85         if i & 1:
    86             ctx1 = ctx1 + pw
    86             ctx1 = ctx1 + pw
    87         else:
    87         else:
    88             ctx1 = ctx1 + final[:16]
    88             ctx1 = ctx1 + final[:16]