-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd4.py
101 lines (87 loc) · 3.72 KB
/
md4.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
97
98
99
100
101
# MD4 Python 3 implementation
# 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 LGPL work by
# Dmitry Rozmanov <[email protected]> 2002
# http://www.geocities.com/rozmanov/python/
import struct
import binascii
lrot = lambda x, n: (x << n) | (x >> (32 - n))
class MD4():
buf = [0x00] * 64
_F = lambda self, x, y, z: ((x & y) | (~x & z))
_G = lambda self, x, y, z: ((x & y) | (x & z) | (y & z))
_H = lambda self, x, y, z: (x ^ y ^ z)
def __init__(self, message, ml = 0,
A = 0x67452301,
B = 0xefcdab89,
C = 0x98badcfe,
D = 0x10325476):
self.A, self.B, self.C, self.D = A, B, C, D
if ml == 0:
ml = len(message)
length = struct.pack('<Q', ml * 8)
while len(message) > 64:
self._handle(message[:64])
message = message[64:]
message += b'\x80'
message += bytes((56 - len(message) % 64) % 64)
message += length
while len(message):
self._handle(message[:64])
message = message[64:]
def _handle(self, chunk):
X = list(struct.unpack('<' + 'I' * 16, chunk))
A, B, C, D = self.A, self.B, self.C, self.D
for i in range(16):
k = i
if i % 4 == 0:
A = lrot((A + self._F(B, C, D) + X[k]) & 0xffffffff, 3)
elif i % 4 == 1:
D = lrot((D + self._F(A, B, C) + X[k]) & 0xffffffff, 7)
elif i % 4 == 2:
C = lrot((C + self._F(D, A, B) + X[k]) & 0xffffffff, 11)
elif i % 4 == 3:
B = lrot((B + self._F(C, D, A) + X[k]) & 0xffffffff, 19)
for i in range(16):
k = (i // 4) + (i % 4) * 4
if i % 4 == 0:
A = lrot((A + self._G(B, C, D) + X[k] + 0x5a827999) & 0xffffffff, 3)
elif i % 4 == 1:
D = lrot((D + self._G(A, B, C) + X[k] + 0x5a827999) & 0xffffffff, 5)
elif i % 4 == 2:
C = lrot((C + self._G(D, A, B) + X[k] + 0x5a827999) & 0xffffffff, 9)
elif i % 4 == 3:
B = lrot((B + self._G(C, D, A) + X[k] + 0x5a827999) & 0xffffffff, 13)
order = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
for i in range(16):
k = order[i]
if i % 4 == 0:
A = lrot((A + self._H(B, C, D) + X[k] + 0x6ed9eba1) & 0xffffffff, 3)
elif i % 4 == 1:
D = lrot((D + self._H(A, B, C) + X[k] + 0x6ed9eba1) & 0xffffffff, 9)
elif i % 4 == 2:
C = lrot((C + self._H(D, A, B) + X[k] + 0x6ed9eba1) & 0xffffffff, 11)
elif i % 4 == 3:
B = lrot((B + self._H(C, D, A) + X[k] + 0x6ed9eba1) & 0xffffffff, 15)
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()