makeMD5.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import math
  2. def F(x,y,z):
  3. return (x&y) | (~x&z)
  4. def G(x,y,z):
  5. return (x&z) | (y&~z)
  6. def H(x,y,z):
  7. return x ^ y ^z
  8. def I(x,y,z):
  9. return y ^ (x | ~z)
  10. def get_s16(val):
  11. if val < 0x80000000:
  12. return val
  13. else:
  14. return (val - 0x100000000)
  15. def XX(a,b,c,d,k,i,s,_func):
  16. a = a+ (_func(b,c,d)&0xFFFFFFFF)+k+get_s16(i)
  17. a = ((a&0xFFFFFFFF)<<s) | ((a&0xFFFFFFFF)>>(32-s))
  18. a += b
  19. return a
  20. def text2hex(_str):
  21. list_hex = []
  22. for _hex in map(hex,_str.encode()):
  23. _i = int(_hex[2:],16)
  24. while(True):
  25. a = _i&0xFF
  26. list_hex.append(hex(a)[2:].rjust(2,"0"))
  27. _i = _i >>8
  28. if _i==0:
  29. break
  30. return "".join(list_hex)
  31. def paddingBits(_str):
  32. padding_length = (512-(len(_str)*4+64)%512)//4
  33. _str += "8"+"0"*(padding_length-1)
  34. return _str
  35. def paddingLength(_str,length):
  36. bit_length = length&0xFFFFFFFFFFFFFFFF
  37. padding_str = ""
  38. for i in range(8):
  39. padding_str += hex(bit_length&0xFF)[2:].rjust(2,"0")
  40. bit_length = bit_length >> 8
  41. _str += padding_str
  42. return _str
  43. def byte2unsign(b):
  44. return b & 0x7F + 128 if b<0 else b
  45. def hex_lowFirst(_str):
  46. _result = 0
  47. for _i in range(len(_str)//2):
  48. _result = _result | (int(_str[_i*2:(_i+1)*2],16)<<(8*_i))
  49. return _result
  50. def trans(A,B,C,D,_str):
  51. list_s = [[7,12,17,22],[5,9,14,20],[4,11,16,23],[6,10,15,21]]
  52. list_k = [int(math.floor(abs(math.sin(i+1))*(2**32))) for i in range(64)]
  53. list_begin_step = [[0,1],[1,5],[5,3],[0,7]]
  54. list_hex = [hex_lowFirst(_str[i*8:(i+1)*8]) for i in range(16)]
  55. # list_hex.reverse()
  56. list_ABCD = [A,B,C,D]
  57. _A,_B,_C,_D = list_ABCD
  58. list_Func = [F,G,H,I]
  59. #四轮计算
  60. for i in range(4):
  61. _s = list_s[i]
  62. _k = list_k[i*16:(i+1)*16]
  63. _step_begin,_step = list_begin_step[i]
  64. _s_begin = -1
  65. _func = list_Func[i]
  66. ABCD_begin = 1
  67. for j in range(16):
  68. _s_begin = (_s_begin+1)%4
  69. ABCD_begin = (ABCD_begin-1)%4
  70. _begin = (_step_begin+j*_step)%16
  71. list_ABCD[ABCD_begin] = XX(list_ABCD[ABCD_begin],list_ABCD[(ABCD_begin+1)%4],list_ABCD[(ABCD_begin+2)%4],list_ABCD[(ABCD_begin+3)%4],list_hex[_begin],_k[j],_s[_s_begin],_func)
  72. list_ABCD[0] += _A
  73. list_ABCD[1] += _B
  74. list_ABCD[2] += _C
  75. list_ABCD[3] += _D
  76. return list_ABCD
  77. def getMD5(_str):
  78. A = 0x67452301
  79. B = 0xefcdab89
  80. C = 0x98badcfe
  81. D = 0x10325476
  82. hex_str = text2hex(_str)
  83. _length = len(hex_str)*4
  84. hex_str = paddingBits(hex_str)
  85. hex_str = paddingLength(hex_str,_length)
  86. for i in range(len(hex_str)//128):
  87. _str = hex_str[i*128:(i+1)*128]
  88. A,B,C,D = trans(A,B,C,D,_str)
  89. mdf_str = ""
  90. for _i in [A,B,C,D]:
  91. list_j = []
  92. list_j.append(hex(_i&0xFF)[2:].rjust(2,'0'))
  93. list_j.append(hex((_i>>8)&0xFF)[2:].rjust(2,'0'))
  94. list_j.append(hex((_i>>16)&0xFF)[2:].rjust(2,'0'))
  95. list_j.append(hex((_i>>24)&0xFF)[2:].rjust(2,'0'))
  96. # _j = hex(_i)[2:].rjust(8,'0')
  97. # for h in range(len(_j)//2):
  98. # list_j.append(_j[h*2:(h+1)*2])
  99. # list_j.reverse()
  100. mdf_str += "".join(list_j)
  101. return mdf_str
  102. if __name__=="__main__":
  103. import codecs
  104. sourceHtml = codecs.open("C:\\Users\\User\\Desktop\\2.html","r",encoding="utf8").read()
  105. # sourceHtml = "abcddafafffffffffffffffffffffffff你"
  106. print(getMD5(sourceHtml))