Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sin
- BLOCK_SIZE = 512
- EQ = 448
- LENGTH_SIZE = 64
- WORD_SIZE = 32
- REG = int('1' * WORD_SIZE, 2)
- def neg(x):
- return x ^ REG
- def to_left(val, k):
- return ((val << k) | (val >> (32 - k))) & REG
- def F(x, y, z):
- return (x & y) | (neg(x) & z)
- def G(x, y, z):
- return (x & z) | (y & neg(z))
- def H(x, y, z):
- return x ^ y ^ z
- def I(x, y, z):
- return y ^ (x | neg(z))
- def T(i):
- return int((1 << 32) * abs(sin(i))) & REG
- def add(x, y):
- return (x + y) & REG
- class MD5Solver(object):
- def __init__(self):
- self.A = 0x67452301
- self.B = 0xefcdab89
- self.C = 0x98badcfe
- self.D = 0x10325476
- @staticmethod
- def round1(regs, words):
- iterations = [
- # 0123 - ABCD 3012 - DABC 2301 - CDAB 1230 - BCDA
- ['0123', 0, 7, 1], ['3012', 1, 12, 2], ['2301', 2, 17, 3], ['1230', 3, 22, 4],
- ['0123', 4, 7, 5], ['3012', 5, 12, 6], ['2301', 6, 17, 7], ['1230', 7, 22, 8],
- ['0123', 8, 7, 9], ['3012', 9, 12, 10], ['2301', 10, 17, 11], ['1230', 11, 22, 12],
- ['0123', 12, 7, 13], ['3012', 13, 12, 14], ['2301', 14, 17, 15], ['1230', 15, 22, 16]
- ]
- def f(iteration):
- order, k, s, i = iteration
- indexes = [int(i) for i in order]
- val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
- for item in iterations:
- f(item)
- return regs
- @staticmethod
- def round2(regs, words):
- iterations = [
- # 0123 - ABCD 3012 - DABC 2301 - CDAB 1230 - BCDA
- ['0123', 1, 5, 17], ['3012', 6, 9, 18], ['2301', 11, 14, 19], ['1230', 0, 20, 20],
- ['0123', 5, 5, 21], ['3012', 10, 9, 22], ['2301', 15, 14, 23], ['1230', 4, 20, 24],
- ['0123', 9, 5, 25], ['3012', 14, 9, 26], ['2301', 3, 14, 27], ['1230', 8, 20, 28],
- ['0123', 13, 5, 29], ['3012', 2, 9, 30], ['2301', 7, 14, 31], ['1230', 12, 20, 32]
- ]
- def f(iteration):
- order, k, s, i = iteration
- indexes = [int(i) for i in order]
- val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
- for item in iterations:
- f(item)
- return regs
- @staticmethod
- def round3(regs, words):
- iterations = [
- # 0123 - ABCD 3012 - DABC 2301 - CDAB 1230 - BCDA
- ['0123', 5, 4, 33], ['3012', 8, 11, 34], ['2301', 11, 16, 35], ['1230', 14, 23, 36],
- ['0123', 1, 4, 37], ['3012', 4, 11, 38], ['2301', 7, 16, 39], ['1230', 10, 23, 40],
- ['0123', 13, 4, 41], ['3012', 0, 11, 42], ['2301', 3, 16, 43], ['1230', 6, 23, 44],
- ['0123', 9, 4, 45], ['3012', 12, 11, 46], ['2301', 15, 16, 47], ['1230', 2, 23, 48]
- ]
- def f(iteration):
- order, k, s, i = iteration
- indexes = [int(i) for i in order]
- val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
- for item in iterations:
- f(item)
- return regs
- @staticmethod
- def round4(regs, words):
- iterations = [
- # 0123 - ABCD 3012 - DABC 2301 - CDAB 1230 - BCDA
- ['0123', 0, 6, 49], ['3012', 7, 10, 50], ['2301', 14, 15, 51], ['1230', 5, 21, 52],
- ['0123', 12, 6, 53], ['3012', 3, 10, 54], ['2301', 10, 15, 55], ['1230', 1, 21, 56],
- ['0123', 8, 6, 57], ['3012', 15, 10, 58], ['2301', 6, 15, 59], ['1230', 13, 21, 60],
- ['0123', 4, 6, 61], ['3012', 11, 10, 62], ['2301', 2, 15, 63], ['1230', 9, 21, 64]
- ]
- def f(iteration):
- order, k, s, i = iteration
- indexes = [int(i) for i in order]
- val = add(regs[indexes[0]], F(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- regs[indexes[0]] = add(regs[indexes[1]], to_left(add(add(val, int(words[k], 2)), T(i)), s))
- for item in iterations:
- f(item)
- return regs
- def get_hash(self, text):
- A, B, C, D = self.A, self.B, self.C, self.D
- flow = str.join('', [str.join('', [format(ord(c), 'b') for c in text]), '1'])
- while len(flow) % BLOCK_SIZE != EQ:
- flow = str.join('', [flow, '0'])
- input_len = format(len(text), 'b')
- while len(input_len) < LENGTH_SIZE:
- input_len = str.join('', ['0', input_len])
- for i in range(8):
- input_len = input_len[::-1]
- byte = input_len[: 8][::-1]
- input_len = input_len[8:][::-1]
- flow = str.join('', [flow, byte])
- while len(flow) > 0:
- AA, BB, CC, DD = A, B, C, D
- block = flow[:BLOCK_SIZE]
- flow = flow[BLOCK_SIZE:]
- words = [block[WORD_SIZE * ind: WORD_SIZE * (ind + 1)] for ind in range(BLOCK_SIZE // WORD_SIZE)]
- A, B, C, D = self.round1([A, B, C, D], words)
- A, B, C, D = self.round2([A, B, C, D], words)
- A, B, C, D = self.round3([A, B, C, D], words)
- A, B, C, D = self.round4([A, B, C, D], words)
- A = add(A, AA)
- B = add(B, BB)
- C = add(C, CC)
- D = add(D, DD)
- print('Input: ', text)
- print('Binary: ', str.join(' ', [format(val, 'b') for val in [A, B, C, D]]))
- print('Hex: ', str.join(' ', [hex(int(format(val, 'b'), 2)) for val in [A, B, C, D]]))
- 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]]))
- print('Whole Hex: ', hex(int(str.join('', [format(val, 'b') for val in [A, B, C, D]]), 2)))
- return str.join('', [format(val, 'b') for val in [A, B, C, D]])
- if __name__ == '__main__':
- solver = MD5Solver()
- #solver.get_hash('Kevin van Zonneveld')
- #print('Ans: ', '6e658d4bfcb59cc13f96c14450ac40b9', '\n')
- print(solver.get_hash(''), '\n')
- test = format(0x1BC29B36F623BA82AAF6724FD3B16718, 'b')
- print(test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement