123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import math
- def F(x,y,z):
- return (x&y) | (~x&z)
- def G(x,y,z):
- return (x&z) | (y&~z)
- def H(x,y,z):
- return x ^ y ^z
- def I(x,y,z):
- return y ^ (x | ~z)
- def get_s16(val):
- if val < 0x80000000:
- return val
- else:
- return (val - 0x100000000)
- def XX(a,b,c,d,k,i,s,_func):
- a = a+ (_func(b,c,d)&0xFFFFFFFF)+k+get_s16(i)
- a = ((a&0xFFFFFFFF)<<s) | ((a&0xFFFFFFFF)>>(32-s))
- a += b
- return a
- def text2hex(_str):
- list_hex = []
- for _hex in map(hex,_str.encode()):
- _i = int(_hex[2:],16)
- while(True):
- a = _i&0xFF
- list_hex.append(hex(a)[2:].rjust(2,"0"))
- _i = _i >>8
- if _i==0:
- break
- return "".join(list_hex)
- def paddingBits(_str):
- padding_length = (512-(len(_str)*4+64)%512)//4
- _str += "8"+"0"*(padding_length-1)
- return _str
- def paddingLength(_str,length):
- bit_length = length&0xFFFFFFFFFFFFFFFF
- padding_str = ""
- for i in range(8):
- padding_str += hex(bit_length&0xFF)[2:].rjust(2,"0")
- bit_length = bit_length >> 8
- _str += padding_str
- return _str
- def byte2unsign(b):
- return b & 0x7F + 128 if b<0 else b
- def hex_lowFirst(_str):
- _result = 0
- for _i in range(len(_str)//2):
- _result = _result | (int(_str[_i*2:(_i+1)*2],16)<<(8*_i))
- return _result
- def trans(A,B,C,D,_str):
- list_s = [[7,12,17,22],[5,9,14,20],[4,11,16,23],[6,10,15,21]]
- list_k = [int(math.floor(abs(math.sin(i+1))*(2**32))) for i in range(64)]
- list_begin_step = [[0,1],[1,5],[5,3],[0,7]]
- list_hex = [hex_lowFirst(_str[i*8:(i+1)*8]) for i in range(16)]
- # list_hex.reverse()
- list_ABCD = [A,B,C,D]
- _A,_B,_C,_D = list_ABCD
- list_Func = [F,G,H,I]
- #四轮计算
- for i in range(4):
- _s = list_s[i]
- _k = list_k[i*16:(i+1)*16]
- _step_begin,_step = list_begin_step[i]
- _s_begin = -1
- _func = list_Func[i]
- ABCD_begin = 1
- for j in range(16):
- _s_begin = (_s_begin+1)%4
- ABCD_begin = (ABCD_begin-1)%4
- _begin = (_step_begin+j*_step)%16
- 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)
- list_ABCD[0] += _A
- list_ABCD[1] += _B
- list_ABCD[2] += _C
- list_ABCD[3] += _D
- return list_ABCD
- def getMD5(_str):
- A = 0x67452301
- B = 0xefcdab89
- C = 0x98badcfe
- D = 0x10325476
- hex_str = text2hex(_str)
- _length = len(hex_str)*4
- hex_str = paddingBits(hex_str)
- hex_str = paddingLength(hex_str,_length)
- for i in range(len(hex_str)//128):
- _str = hex_str[i*128:(i+1)*128]
- A,B,C,D = trans(A,B,C,D,_str)
- mdf_str = ""
- for _i in [A,B,C,D]:
- list_j = []
- list_j.append(hex(_i&0xFF)[2:].rjust(2,'0'))
- list_j.append(hex((_i>>8)&0xFF)[2:].rjust(2,'0'))
- list_j.append(hex((_i>>16)&0xFF)[2:].rjust(2,'0'))
- list_j.append(hex((_i>>24)&0xFF)[2:].rjust(2,'0'))
- # _j = hex(_i)[2:].rjust(8,'0')
- # for h in range(len(_j)//2):
- # list_j.append(_j[h*2:(h+1)*2])
- # list_j.reverse()
- mdf_str += "".join(list_j)
- return mdf_str
- if __name__=="__main__":
- import codecs
- sourceHtml = codecs.open("C:\\Users\\User\\Desktop\\2.html","r",encoding="utf8").read()
- # sourceHtml = "abcddafafffffffffffffffffffffffff你"
- print(getMD5(sourceHtml))
|