Advertisement
MarkUa

Untitled

Oct 6th, 2019
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1.  
  2. # !/usr/bin/python3
  3. import binascii
  4. import sys
  5. import os.path
  6.  
  7. SV = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf,
  8.       0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af,
  9.       0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e,
  10.       0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  11.       0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6,
  12.       0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
  13.       0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122,
  14.       0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  15.       0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, 0xd9d4d039,
  16.       0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97,
  17.       0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d,
  18.       0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  19.       0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391]
  20.  
  21.  
  22. def leftCircularShift(k, bits):
  23.     bits = bits % 32
  24.     k = k % (2 ** 32)
  25.     upper = (k << bits) % (2 ** 32)
  26.     result = upper | (k >> (32 - (bits)))
  27.     return (result)
  28.  
  29.  
  30. def blockDivide(block, chunks):
  31.     result = []
  32.     size = len(block) // chunks
  33.     for i in range(0, chunks):
  34.         result.append(int.from_bytes(block[i * size:(i + 1) * size], byteorder="little"))
  35.     return (result)
  36.  
  37.  
  38. def F(X, Y, Z):
  39.     return ((X & Y) | ((~X) & Z))
  40.  
  41.  
  42. def G(X, Y, Z):
  43.     return ((X & Z) | (Y & (~Z)))
  44.  
  45.  
  46. def H(X, Y, Z):
  47.     return (X ^ Y ^ Z)
  48.  
  49.  
  50. def I(X, Y, Z):
  51.     return (Y ^ (X | (~Z)))
  52.  
  53.  
  54. def FF(a, b, c, d, M, s, t):
  55.     result = b + leftCircularShift((a + F(b, c, d) + M + t), s)
  56.     return (result)
  57.  
  58.  
  59. def GG(a, b, c, d, M, s, t):
  60.     result = b + leftCircularShift((a + G(b, c, d) + M + t), s)
  61.     return (result)
  62.  
  63.  
  64. def HH(a, b, c, d, M, s, t):
  65.     result = b + leftCircularShift((a + H(b, c, d) + M + t), s)
  66.     return (result)
  67.  
  68.  
  69. def II(a, b, c, d, M, s, t):
  70.     result = b + leftCircularShift((a + I(b, c, d) + M + t), s)
  71.     return (result)
  72.  
  73.  
  74. def fmt8(num):
  75.     bighex = "{0:08x}".format(num)
  76.     binver = binascii.unhexlify(bighex)
  77.     result = "{0:08x}".format(int.from_bytes(binver, byteorder='little'))
  78.     return (result)
  79.  
  80.  
  81. def bitlen(bitstring):
  82.     return (len(bitstring) * 8)
  83.  
  84.  
  85. def md5sum(msg):
  86.     # First, we pad the message
  87.     msgLen = bitlen(msg)
  88.     print(msgLen)
  89.     msg = bytes(msg, encoding='utf-8') + b'\x80'
  90.     zeroPad = (448 - (msgLen + 8) % 512) % 512
  91.     zeroPad //= 8
  92.     msg = msg + b'\x00' * zeroPad + msgLen.to_bytes(8, byteorder='little')
  93.     msgLen = bitlen(msg)
  94.     iterations = msgLen // 512
  95.     # chaining variables
  96.     A = 0x67452301
  97.     B = 0xefcdab89
  98.     C = 0x98badcfe
  99.     D = 0x10325476
  100.     # main loop
  101.     for i in range(0, iterations):
  102.         a = A
  103.         b = B
  104.         c = C
  105.         d = D
  106.         block = msg[i * 64:(i + 1) * 64]
  107.         M = blockDivide(block, 16)
  108.         # Rounds
  109.         a = FF(a, b, c, d, M[0], 7, SV[0])
  110.         d = FF(d, a, b, c, M[1], 12, SV[1])
  111.         c = FF(c, d, a, b, M[2], 17, SV[2])
  112.         b = FF(b, c, d, a, M[3], 22, SV[3])
  113.         a = FF(a, b, c, d, M[4], 7, SV[4])
  114.         d = FF(d, a, b, c, M[5], 12, SV[5])
  115.         c = FF(c, d, a, b, M[6], 17, SV[6])
  116.         b = FF(b, c, d, a, M[7], 22, SV[7])
  117.         a = FF(a, b, c, d, M[8], 7, SV[8])
  118.         d = FF(d, a, b, c, M[9], 12, SV[9])
  119.         c = FF(c, d, a, b, M[10], 17, SV[10])
  120.         b = FF(b, c, d, a, M[11], 22, SV[11])
  121.         a = FF(a, b, c, d, M[12], 7, SV[12])
  122.         d = FF(d, a, b, c, M[13], 12, SV[13])
  123.         c = FF(c, d, a, b, M[14], 17, SV[14])
  124.         b = FF(b, c, d, a, M[15], 22, SV[15])
  125.         a = GG(a, b, c, d, M[1], 5, SV[16])
  126.         d = GG(d, a, b, c, M[6], 9, SV[17])
  127.         c = GG(c, d, a, b, M[11], 14, SV[18])
  128.         b = GG(b, c, d, a, M[0], 20, SV[19])
  129.         a = GG(a, b, c, d, M[5], 5, SV[20])
  130.         d = GG(d, a, b, c, M[10], 9, SV[21])
  131.         c = GG(c, d, a, b, M[15], 14, SV[22])
  132.         b = GG(b, c, d, a, M[4], 20, SV[23])
  133.         a = GG(a, b, c, d, M[9], 5, SV[24])
  134.         d = GG(d, a, b, c, M[14], 9, SV[25])
  135.         c = GG(c, d, a, b, M[3], 14, SV[26])
  136.         b = GG(b, c, d, a, M[8], 20, SV[27])
  137.         a = GG(a, b, c, d, M[13], 5, SV[28])
  138.         d = GG(d, a, b, c, M[2], 9, SV[29])
  139.         c = GG(c, d, a, b, M[7], 14, SV[30])
  140.         b = GG(b, c, d, a, M[12], 20, SV[31])
  141.         a = HH(a, b, c, d, M[5], 4, SV[32])
  142.         d = HH(d, a, b, c, M[8], 11, SV[33])
  143.         c = HH(c, d, a, b, M[11], 16, SV[34])
  144.         b = HH(b, c, d, a, M[14], 23, SV[35])
  145.         a = HH(a, b, c, d, M[1], 4, SV[36])
  146.         d = HH(d, a, b, c, M[4], 11, SV[37])
  147.         c = HH(c, d, a, b, M[7], 16, SV[38])
  148.         b = HH(b, c, d, a, M[10], 23, SV[39])
  149.         a = HH(a, b, c, d, M[13], 4, SV[40])
  150.         d = HH(d, a, b, c, M[0], 11, SV[41])
  151.         c = HH(c, d, a, b, M[3], 16, SV[42])
  152.         b = HH(b, c, d, a, M[6], 23, SV[43])
  153.         a = HH(a, b, c, d, M[9], 4, SV[44])
  154.         d = HH(d, a, b, c, M[12], 11, SV[45])
  155.         c = HH(c, d, a, b, M[15], 16, SV[46])
  156.         b = HH(b, c, d, a, M[2], 23, SV[47])
  157.         a = II(a, b, c, d, M[0], 6, SV[48])
  158.         d = II(d, a, b, c, M[7], 10, SV[49])
  159.         c = II(c, d, a, b, M[14], 15, SV[50])
  160.         b = II(b, c, d, a, M[5], 21, SV[51])
  161.         a = II(a, b, c, d, M[12], 6, SV[52])
  162.         d = II(d, a, b, c, M[3], 10, SV[53])
  163.         c = II(c, d, a, b, M[10], 15, SV[54])
  164.         b = II(b, c, d, a, M[1], 21, SV[55])
  165.         a = II(a, b, c, d, M[8], 6, SV[56])
  166.         d = II(d, a, b, c, M[15], 10, SV[57])
  167.         c = II(c, d, a, b, M[6], 15, SV[58])
  168.         b = II(b, c, d, a, M[13], 21, SV[59])
  169.         a = II(a, b, c, d, M[4], 6, SV[60])
  170.         d = II(d, a, b, c, M[11], 10, SV[61])
  171.         c = II(c, d, a, b, M[2], 15, SV[62])
  172.         b = II(b, c, d, a, M[9], 21, SV[63])
  173.         A = (A + a) % (2 ** 32)
  174.         B = (B + b) % (2 ** 32)
  175.         C = (C + c) % (2 ** 32)
  176.         D = (D + d) % (2 ** 32)
  177.     result = fmt8(A) + fmt8(B) + fmt8(C) + fmt8(D)
  178.     return (result)
  179.  
  180. print(str(md5sum("ddd")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement