Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.55 KB | None | 0 0
  1. from math import sin
  2.  
  3.  
  4. BLOCK_SIZE = 512
  5. EQ = 448
  6. LENGTH_SIZE = 64
  7. WORD_SIZE = 32
  8. REG = int('1' * WORD_SIZE, 2)
  9.  
  10.  
  11. def neg(x):
  12.     return x ^ REG
  13.  
  14.  
  15. def to_left(val, k):
  16.     return ((val << k) | (val >> (32 - k))) & REG
  17.  
  18.  
  19. def F(x, y, z):
  20.     return (x & y) | (neg(x) & z)
  21.  
  22.  
  23. def G(x, y, z):
  24.     return (x & z) | (y & neg(z))
  25.  
  26.  
  27. def H(x, y, z):
  28.     return x ^ y ^ z
  29.  
  30.  
  31. def I(x, y, z):
  32.     return y ^ (x | neg(z))
  33.  
  34.  
  35. def T(i):
  36.     return int((1 << 32) * abs(sin(i))) & REG
  37.  
  38.  
  39. def add(x, y):
  40.     return (x + y) & REG
  41.  
  42.  
  43. class MD5Solver(object):
  44.     def __init__(self):
  45.         self.A = 0x67452301
  46.         self.B = 0xefcdab89
  47.         self.C = 0x98badcfe
  48.         self.D = 0x10325476
  49.  
  50.     @staticmethod
  51.     def round1(regs, words):
  52.         iterations = [
  53.             # 0123 - ABCD          3012 - DABC           2301 - CDAB           1230 - BCDA
  54.             ['0123',  0, 7,  1], ['3012',  1, 12,  2], ['2301',  2, 17,  3], ['1230',  3, 22,  4],
  55.             ['0123',  4, 7,  5], ['3012',  5, 12,  6], ['2301',  6, 17,  7], ['1230',  7, 22,  8],
  56.             ['0123',  8, 7,  9], ['3012',  9, 12, 10], ['2301', 10, 17, 11], ['1230', 11, 22, 12],
  57.             ['0123', 12, 7, 13], ['3012', 13, 12, 14], ['2301', 14, 17, 15], ['1230', 15, 22, 16]
  58.         ]
  59.  
  60.         def f(iteration):
  61.             order, k, s, i = iteration
  62.             indexes = [int(i) for i in order]
  63.             val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
  64.             regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
  65.  
  66.         for item in iterations:
  67.             f(item)
  68.  
  69.         return regs
  70.  
  71.     @staticmethod
  72.     def round2(regs, words):
  73.         iterations = [
  74.             # 0123 - ABCD          3012 - DABC           2301 - CDAB           1230 - BCDA
  75.             ['0123',  1, 5, 17], ['3012',  6, 9, 18], ['2301', 11, 14, 19], ['1230',  0, 20, 20],
  76.             ['0123',  5, 5, 21], ['3012', 10, 9, 22], ['2301', 15, 14, 23], ['1230',  4, 20, 24],
  77.             ['0123',  9, 5, 25], ['3012', 14, 9, 26], ['2301',  3, 14, 27], ['1230',  8, 20, 28],
  78.             ['0123', 13, 5, 29], ['3012',  2, 9, 30], ['2301',  7, 14, 31], ['1230', 12, 20, 32]
  79.         ]
  80.  
  81.         def f(iteration):
  82.             order, k, s, i = iteration
  83.             indexes = [int(i) for i in order]
  84.             val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
  85.             regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
  86.  
  87.         for item in iterations:
  88.             f(item)
  89.  
  90.         return regs
  91.  
  92.     @staticmethod
  93.     def round3(regs, words):
  94.         iterations = [
  95.             # 0123 - ABCD          3012 - DABC           2301 - CDAB           1230 - BCDA
  96.             ['0123', 5,  4, 33], ['3012', 8,  11, 34], ['2301', 11, 16, 35], ['1230', 14, 23, 36],
  97.             ['0123', 1,  4, 37], ['3012', 4,  11, 38], ['2301', 7,  16, 39], ['1230', 10, 23, 40],
  98.             ['0123', 13, 4, 41], ['3012', 0,  11, 42], ['2301', 3,  16, 43], ['1230', 6,  23, 44],
  99.             ['0123', 9,  4, 45], ['3012', 12, 11, 46], ['2301', 15, 16, 47], ['1230', 2,  23, 48]
  100.         ]
  101.  
  102.         def f(iteration):
  103.             order, k, s, i = iteration
  104.             indexes = [int(i) for i in order]
  105.             val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
  106.             regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
  107.  
  108.         for item in iterations:
  109.             f(item)
  110.  
  111.         return regs
  112.  
  113.     @staticmethod
  114.     def round4(regs, words):
  115.         iterations = [
  116.             # 0123 - ABCD          3012 - DABC           2301 - CDAB           1230 - BCDA
  117.             ['0123', 0,  6, 49], ['3012', 7,  10, 50], ['2301', 14, 15, 51], ['1230', 5,  21, 52],
  118.             ['0123', 12, 6, 53], ['3012', 3,  10, 54], ['2301', 10, 15, 55], ['1230', 1,  21, 56],
  119.             ['0123', 8,  6, 57], ['3012', 15, 10, 58], ['2301', 6,  15, 59], ['1230', 13, 21, 60],
  120.             ['0123', 4,  6, 61], ['3012', 11, 10, 62], ['2301', 2,  15, 63], ['1230', 9,  21, 64]
  121.         ]
  122.  
  123.         def f(iteration):
  124.             order, k, s, i = iteration
  125.             indexes = [int(i) for i in order]
  126.             val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
  127.             regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
  128.  
  129.         for item in iterations:
  130.             f(item)
  131.  
  132.         return regs
  133.  
  134.     def get_hash(self, text):
  135.         A, B, C, D = self.A, self.B, self.C, self.D
  136.  
  137.         flow = str.join('', [str.join('', [format(ord(c), 'b') for c in text]), '1'])
  138.  
  139.         while len(flow) % BLOCK_SIZE != EQ:
  140.             flow = str.join('', [flow, '0'])
  141.  
  142.         input_len = format(len(text), 'b')
  143.  
  144.         while len(input_len) < LENGTH_SIZE:
  145.             input_len = str.join('', ['0', input_len])
  146.  
  147.         for i in range(8):
  148.  
  149.             input_len = input_len[::-1]
  150.             byte = input_len[: 8][::-1]
  151.             input_len = input_len[8:][::-1]
  152.            
  153.             flow = str.join('', [flow, byte])
  154.  
  155.         while len(flow) > 0:
  156.             AA, BB, CC, DD = A, B, C, D
  157.            
  158.             block = flow[:BLOCK_SIZE]
  159.             flow = flow[BLOCK_SIZE:]
  160.             words = [block[WORD_SIZE * ind: WORD_SIZE * (ind + 1)] for ind in range(BLOCK_SIZE // WORD_SIZE)]
  161.  
  162.             A, B, C, D = self.round1([A, B, C, D], words)
  163.             A, B, C, D = self.round2([A, B, C, D], words)
  164.             A, B, C, D = self.round3([A, B, C, D], words)
  165.             A, B, C, D = self.round4([A, B, C, D], words)
  166.  
  167.             A = add(A, AA)
  168.             B = add(B, BB)
  169.             C = add(C, CC)
  170.             D = add(D, DD)
  171.  
  172.         print('Input: ', text)
  173.         print('Binary: ', str.join(' ', [format(val, 'b') for val in [A, B, C, D]]))
  174.         print('Hex: ', str.join(' ', [hex(int(format(val, 'b'), 2)) for val in [A, B, C, D]]))
  175.         print('Byte Hex: ', str.join(' ', [hex(int(format(val, 'b')[::-1][8 * ind: 8 * ind + 8][::-1], 2)) for ind in range(4) for val in [A, B, C, D]]))
  176.         print('Whole Hex: ', hex(int(str.join('', [format(val, 'b') for val in [A, B, C, D]]), 2)))
  177.  
  178.         return str.join('', [format(val, 'b') for val in [A, B, C, D]])
  179.  
  180. if __name__ == '__main__':
  181.     solver = MD5Solver()
  182.  
  183.     #solver.get_hash('Kevin van Zonneveld')
  184.     #print('Ans: ', '6e658d4bfcb59cc13f96c14450ac40b9', '\n')
  185.  
  186.     print(solver.get_hash(''), '\n')
  187.  
  188.     test = format(0x1BC29B36F623BA82AAF6724FD3B16718, 'b')
  189.     print(test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement