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 F(x, y, z):
- return (x & y) | ((not x) & z)
- def G(x, y, z):
- return (x & z) | (y & (not z))
- def H(x, y, z):
- return x ^ y ^ z
- def I(x, y, z):
- return y ^ (x | (not z))
- def test_funcs():
- print([F(x, y, z) for x in range(2) for y in range(2) for z in range(2)])
- print([G(x, y, z) for x in range(2) for y in range(2) for z in range(2)])
- print([H(x, y, z) for x in range(2) for y in range(2) for z in range(2)])
- print([I(x, y, z) for x in range(2) for y in range(2) for z in range(2)], '\n')
- def T(i):
- return int((1 << 32) * sin(i)) & REG
- def to_left(val, k):
- return ((val << k) | (val >> (32 - k))) & REG
- def add(x, y):
- x8 = x & 0x80000000
- y8 = y & 0x80000000
- x4 = x & 0x40000000
- y4 = y & 0x40000000
- res = (x & 0x3FFFFFFF) + (y & 0x3FFFFFFF)
- if x4 & y4:
- return res ^ 0x80000000 ^ x8 ^ y8
- elif x4 | y4:
- if res & 0x40000000:
- return res ^ 0xC0000000 ^ x8 ^ y8
- else:
- return res ^ 0x40000000 ^ x8 ^ y8
- else:
- return res ^ x8 ^ y8
- 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]]))
- val = add(val, int(words[k], 2))
- val = add(val, T(i))
- val = to_left(val, s)
- regs[indexes[0]] = add(regs[indexes[1]], val)
- 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]], G(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- val = add(val, int(words[k], 2))
- val = add(val, T(i))
- val = to_left(val, s)
- regs[indexes[0]] = add(regs[indexes[1]], val)
- for iteration in iterations:
- f(iteration)
- 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]], H(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- val = add(val, int(words[k], 2))
- val = add(val, T(i))
- val = to_left(val, s)
- regs[indexes[0]] = add(regs[indexes[1]], val)
- for iteration in iterations:
- f(iteration)
- 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]], I(regs[indexes[1]], regs[indexes[2]], regs[indexes[3]]))
- val = add(val, int(words[k], 2))
- val = add(val, T(i))
- val = to_left(val, s)
- regs[indexes[0]] = add(regs[indexes[1]], val)
- for iteration in iterations:
- f(iteration)
- return regs
- def get_hash(self, text):
- A, B, C, D = self.A, self.B, self.C, self.D
- input_len = len(text)
- flow = str.join('', [format(ord(c), 'b') for c in text])
- flow = str.join('', [flow, '1'])
- while len(flow) % BLOCK_SIZE != EQ:
- flow = str.join('', [flow, '0'])
- input_len = format(input_len, 'b')
- while len(input_len) < LENGTH_SIZE:
- input_len = str.join('', ['0', input_len])
- input_len = input_len[::-1]
- input_len = input_len[: LENGTH_SIZE]
- flow = str.join('', [flow, input_len[::-1]])
- while len(flow) > 0:
- block = flow[:BLOCK_SIZE]
- flow = flow[BLOCK_SIZE:]
- old_A, old_B, old_C, old_D = A, B, C, D
- 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, old_A)
- B = add(B, old_B)
- C = add(C, old_C)
- D = add(D, old_D)
- 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()
- test_funcs()
- 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