Skip to content

Instantly share code, notes, and snippets.

@llSourcell
Created March 7, 2018 16:04
Show Gist options
  • Select an option

  • Save llSourcell/cba06fd7142a4e33fc6aebad2bad18c6 to your computer and use it in GitHub Desktop.

Select an option

Save llSourcell/cba06fd7142a4e33fc6aebad2bad18c6 to your computer and use it in GitHub Desktop.
def md5(message):
message = bytearray(message) #copy our input into a mutable buffer
orig_len_in_bits = (8 * len(message)) & 0xffffffffffffffff
message.append(0x80)
while len(message)%64 != 56:
message.append(0)
message += orig_len_in_bits.to_bytes(8, byteorder='little')
hash_pieces = init_values[:]
for chunk_ofst in range(0, len(message), 64):
a, b, c, d = hash_pieces
chunk = message[chunk_ofst:chunk_ofst+64]
for i in range(64):
f = functions[i](b, c, d)
g = index_functions[i](i)
to_rotate = a + f + constants[i] + int.from_bytes(chunk[4*g:4*g+4], byteorder='little')
new_b = (b + left_rotate(to_rotate, rotate_amounts[i])) & 0xFFFFFFFF
a, b, c, d = d, new_b, b, c
for i, val in enumerate([a, b, c, d]):
hash_pieces[i] += val
hash_pieces[i] &= 0xFFFFFFFF
return sum(x<<(32*i) for i, x in enumerate(hash_pieces))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment