cubicweb/md5crypt.py
changeset 12567 26744ad37953
parent 11057 0b59724cb3f2
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    41 MAGIC = b'$1$'                        # Magic string
    41 MAGIC = b'$1$'                        # Magic string
    42 ITOA64 = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    42 ITOA64 = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    43 
    43 
    44 from hashlib import md5 # pylint: disable=E0611
    44 from hashlib import md5 # pylint: disable=E0611
    45 
    45 
    46 from six import text_type, indexbytes
       
    47 from six.moves import range
       
    48 
       
    49 
    46 
    50 def to64 (v, n):
    47 def to64 (v, n):
    51     ret = bytearray()
    48     ret = bytearray()
    52     while (n - 1 >= 0):
    49     while (n - 1 >= 0):
    53         n = n - 1
    50         n = n - 1
    54         ret.append(ITOA64[v & 0x3f])
    51         ret.append(ITOA64[v & 0x3f])
    55         v = v >> 6
    52         v = v >> 6
    56     return ret
    53     return ret
    57 
    54 
    58 def crypt(pw, salt):
    55 def crypt(pw, salt):
    59     if isinstance(pw, text_type):
    56     if isinstance(pw, str):
    60         pw = pw.encode('utf-8')
    57         pw = pw.encode('utf-8')
    61     if isinstance(salt, text_type):
    58     if isinstance(salt, str):
    62         salt = salt.encode('ascii')
    59         salt = salt.encode('ascii')
    63     # Take care of the magic string if present
    60     # Take care of the magic string if present
    64     if salt.startswith(MAGIC):
    61     if salt.startswith(MAGIC):
    65         salt = salt[len(MAGIC):]
    62         salt = salt[len(MAGIC):]
    66     # salt can have up to 8 characters:
    63     # salt can have up to 8 characters:
   100         else:
    97         else:
   101             ctx1 = ctx1 + pw
    98             ctx1 = ctx1 + pw
   102         final = md5(ctx1).digest()
    99         final = md5(ctx1).digest()
   103     # Final xform
   100     # Final xform
   104     passwd = b''
   101     passwd = b''
   105     passwd += to64((indexbytes(final, 0) << 16)
   102     passwd += to64((final[0] << 16)
   106                    |(indexbytes(final, 6) << 8)
   103                    |(final[6] << 8)
   107                    |(indexbytes(final, 12)),4)
   104                    |(final[12]),4)
   108     passwd += to64((indexbytes(final, 1) << 16)
   105     passwd += to64((final[1] << 16)
   109                    |(indexbytes(final, 7) << 8)
   106                    |(final[7] << 8)
   110                    |(indexbytes(final, 13)), 4)
   107                    |(final[13]), 4)
   111     passwd += to64((indexbytes(final, 2) << 16)
   108     passwd += to64((final[2] << 16)
   112                    |(indexbytes(final, 8) << 8)
   109                    |(final[8] << 8)
   113                    |(indexbytes(final, 14)), 4)
   110                    |(final[14]), 4)
   114     passwd += to64((indexbytes(final, 3) << 16)
   111     passwd += to64((final[3] << 16)
   115                    |(indexbytes(final, 9) << 8)
   112                    |(final[9] << 8)
   116                    |(indexbytes(final, 15)), 4)
   113                    |(final[15]), 4)
   117     passwd += to64((indexbytes(final, 4) << 16)
   114     passwd += to64((final[4] << 16)
   118                    |(indexbytes(final, 10) << 8)
   115                    |(final[10] << 8)
   119                    |(indexbytes(final, 5)), 4)
   116                    |(final[5]), 4)
   120     passwd += to64((indexbytes(final, 11)), 2)
   117     passwd += to64((final[11]), 2)
   121     return passwd
   118     return passwd