Skip to content

Instantly share code, notes, and snippets.

Created February 24, 2016 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bdae8b7d60b8e31654ee to your computer and use it in GitHub Desktop.
Save anonymous/bdae8b7d60b8e31654ee to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#Importieren des Bitstringmoduls für einzelne Bitmanipulation sowie des Mathmoduls
#für erweiterte mathematische Funktionen wie Sinus und Floor
from bitstring import BitStream
from math import *
#Zahl um wie viel Bits pro Runde rotiert wird
s = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
#Berechnen der pseudozufälligen Konstante Ki
K = []
for i in range(0, 64):
K.append(floor(abs(sin(i + 1)) * 2**32))
def left_rotate(x, amount):
x &= 0xFFFFFFFF
return ((x<<amount) | (x>>(32-amount))) & 0xFFFFFFFF
def md5(message):
#Konvertieren der Nachricht in einzelne Bits
bits = BitStream(bytes(message, "utf-8"))
old_len = bits.len
#Anhängen eines Bits mit dem Wert 1
bits += [1]
#Anhängen so vieler Bits, bis die Bitlänge der Nachricht kongruent 448 modulo 512 ist.
for i in range(0, 511):
bits += [0]
if(bits.len % 512 == 448):
break
#Anhängen der Länge der Nachricht bevor dem Padding als 64-Bit Representation
old_len_as_bits = BitStream(uintle=old_len, length=64)
bits += old_len_as_bits
#Initialisierungsvektor festlegen
a0 = 0x67452301
b0 = 0xEFCDAB89
c0 = 0x98BADCFE
d0 = 0x10325476
#Durch jeden 512-Bit Block der gepaddeten Nachricht iterieren
for M in bits.cut(512):
#Die 512-Bit Blocks in 16 32-Bit Blöcke unterteilen und diese in der Liste
#split_blocks speichern
split_block = []
for b in M.cut(32):
split_block.append(b)
A = a0
B = b0
C = c0
D = d0
for i in range(0,64):
if(0 <= i <= 15):
F = (B & C) | ((~B) & D)
g = i
elif (16 <= i <= 31):
F = (B & D) | (C & (~D))
g = (5*i + 1) % 16
elif(32 <= i <= 47):
F = B ^ C ^ D
g = (3*i + 5) % 16
elif 48 <= i <= 63:
F = C ^ (B | (~D))
g = (7*i) % 16
mod = 2**32
temp = D
D = C
C = B
rotate = (A + F + K[i] + split_block[g].uintle)
B = (B + left_rotate(rotate,s[i]))%mod
A = temp
a0 = (a0 + A)%mod
b0 = (b0 + B)%mod
c0 = (c0 + C)%mod
d0 = (d0 + D)%mod
print(hex(a0), hex(b0), hex(c0), hex(d0))
print(md5("kappa"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment