Advertisement
Guest User

Untitled

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