-
Notifications
You must be signed in to change notification settings - Fork 3
/
md5.py
98 lines (78 loc) · 3.4 KB
/
md5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# MD5 Python 2 implementation
# coding=UTF-8
# Copyright (C) 2013 Filippo Valsorda
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Originally based, even if no code survives, on a Python license work
# by Dinu C. Gherman (C) 2001 and Aurelian Coman
# http://starship.python.net/crew/gherman/programs/md5py/md5py.py
import struct
import binascii
import math
lrot = lambda x, n: (x << n) | (x >> (32 - n))
class MD5():
A, B, C, D = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476)
# r specifies the per-round shift amounts
r = [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]
# Use binary integer part of the sines of integers (Radians) as constants
k = [int(math.floor(abs(math.sin(i + 1)) * (2 ** 32))) for i in range(64)]
def __init__(self, message):
# MD5 works in 512 bit (64 byte) blocks. Apply padding and process each block.
length = struct.pack('<Q', len(message) * 8)
message += '\x80'
message += '\x00' * ((64 - len(length) - (len(message) % 64)) % 64)
message += length
assert len(message) % 64 == 0
while len(message):
# Bite off whole 64-character chunks and handle them.
self._handle(message[:64])
message = message[64:]
def _handle(self, chunk):
w = list(struct.unpack('<' + 'I' * 16, chunk))
a, b, c, d = self.A, self.B, self.C, self.D
for i in range(64):
if i < 16:
f = (b & c) | ((~b) & d)
g = i
elif i < 32:
f = (d & b) | ((~d) & c)
g = (5 * i + 1) % 16
elif i < 48:
f = b ^ c ^ d
g = (3 * i + 5) % 16
else:
f = c ^ (b | (~d))
g = (7 * i) % 16
x = b + lrot((a + f + self.k[i] + w[g]) & 0xffffffff, self.r[i])
a, b, c, d = d, x & 0xffffffff, b, c
self.A = (self.A + a) & 0xffffffff
self.B = (self.B + b) & 0xffffffff
self.C = (self.C + c) & 0xffffffff
self.D = (self.D + d) & 0xffffffff
def digest(self):
return struct.pack('<IIII', self.A, self.B, self.C, self.D)
def hexdigest(self):
return binascii.hexlify(self.digest()).decode()
if __name__ == "__main__":
s1 = 'thequickbrownfoxjumpedoverthelazydog'
assert MD5(s1).hexdigest() == 'df14e4d5469e801dbc8e1df4eebd97b3'
s2 = '\xff\xff'
assert MD5(s2).hexdigest() == 'ab2a0d28de6b77ffdd6c72afead099ab'
s3 = '😦 😧 😈 👿 😮 😬 😐 😕 😯 😶 😇 😏'
assert MD5(s3).hexdigest() == '83162541e5329195409c925314250bc5'
print 'It works!'