Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1# Copyright (c) 2001 Markus Friedl. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1. Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
13# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23# pylint: skip-file
24
25import sys
26
27digest_size = 20
28digestsize = 20
29
30class RIPEMD160:
31 """
32 Return a new RIPEMD160 object. An optional string argument
33 may be provided; if present, this string will be automatically
34 hashed.
35 """
36
37 def __init__(self, arg=None):
38 self.ctx = RMDContext()
39 if arg:
40 self.update(arg)
41 self.dig = None
42
43 def update(self, arg):
44 RMD160Update(self.ctx, arg, len(arg))
45 self.dig = None
46
47 def digest(self):
48 if self.dig:
49 return self.dig
50 ctx = self.ctx.copy()
51 self.dig = RMD160Final(self.ctx)
52 self.ctx = ctx
53 return self.dig
54
55 def hexdigest(self):
56 dig = self.digest()
57 hex_digest = ""
58 for d in dig:
59 hex_digest += "%02x" % d
60 return hex_digest
61
62 def copy(self):
63 import copy
64 return copy.deepcopy(self)
65
66
67
68def new(arg=None):
69 """
70 Return a new RIPEMD160 object. An optional string argument
71 may be provided; if present, this string will be automatically
72 hashed.
73 """
74 return RIPEMD160(arg)
75
76
77
78#
79# Private.
80#
81
82class RMDContext:
83 def __init__(self):
84 self.state = [0x67452301, 0xEFCDAB89, 0x98BADCFE,
85 0x10325476, 0xC3D2E1F0] # uint32
86 self.count = 0 # uint64
87 self.buffer = [0] * 64 # uchar
88 def copy(self):
89 ctx = RMDContext()
90 ctx.state = self.state[:]
91 ctx.count = self.count
92 ctx.buffer = self.buffer[:]
93 return ctx
94
95K0 = 0x00000000
96K1 = 0x5A827999
97K2 = 0x6ED9EBA1
98K3 = 0x8F1BBCDC
99K4 = 0xA953FD4E
100
101KK0 = 0x50A28BE6
102KK1 = 0x5C4DD124
103KK2 = 0x6D703EF3
104KK3 = 0x7A6D76E9
105KK4 = 0x00000000
106
107def ROL(n, x):
108 return ((x << n) & 0xffffffff) | (x >> (32 - n))
109
110def F0(x, y, z):
111 return x ^ y ^ z
112
113def F1(x, y, z):
114 return (x & y) | (((~x) % 0x100000000) & z)
115
116def F2(x, y, z):
117 return (x | ((~y) % 0x100000000)) ^ z
118
119def F3(x, y, z):
120 return (x & z) | (((~z) % 0x100000000) & y)
121
122def F4(x, y, z):
123 return x ^ (y | ((~z) % 0x100000000))
124
125def R(a, b, c, d, e, Fj, Kj, sj, rj, X):
126 a = ROL(sj, (a + Fj(b, c, d) + X[rj] + Kj) % 0x100000000) + e
127 c = ROL(10, c)
128 return a % 0x100000000, c
129
130PADDING = [0x80] + [0] * 63
131
132import sys
133import struct
134
135def RMD160Transform(state, block): # uint32 state[5], uchar block[64]
136 x = [0] * 16
137 if sys.byteorder == "little":
138 x = struct.unpack("<16L", bytes(block[0:64]))
139 else:
140 raise ValueError("Big-endian platforms are not supported")
141 a = state[0]
142 b = state[1]
143 c = state[2]
144 d = state[3]
145 e = state[4]
146
147 # Round 1
148 a, c = R(a, b, c, d, e, F0, K0, 11, 0, x)
149 e, b = R(e, a, b, c, d, F0, K0, 14, 1, x)
150 d, a = R(d, e, a, b, c, F0, K0, 15, 2, x)
151 c, e = R(c, d, e, a, b, F0, K0, 12, 3, x)
152 b, d = R(b, c, d, e, a, F0, K0, 5, 4, x)
153 a, c = R(a, b, c, d, e, F0, K0, 8, 5, x)
154 e, b = R(e, a, b, c, d, F0, K0, 7, 6, x)
155 d, a = R(d, e, a, b, c, F0, K0, 9, 7, x)
156 c, e = R(c, d, e, a, b, F0, K0, 11, 8, x)
157 b, d = R(b, c, d, e, a, F0, K0, 13, 9, x)
158 a, c = R(a, b, c, d, e, F0, K0, 14, 10, x)
159 e, b = R(e, a, b, c, d, F0, K0, 15, 11, x)
160 d, a = R(d, e, a, b, c, F0, K0, 6, 12, x)
161 c, e = R(c, d, e, a, b, F0, K0, 7, 13, x)
162 b, d = R(b, c, d, e, a, F0, K0, 9, 14, x)
163 a, c = R(a, b, c, d, e, F0, K0, 8, 15, x) # #15
164 # Round 2
165 e, b = R(e, a, b, c, d, F1, K1, 7, 7, x)
166 d, a = R(d, e, a, b, c, F1, K1, 6, 4, x)
167 c, e = R(c, d, e, a, b, F1, K1, 8, 13, x)
168 b, d = R(b, c, d, e, a, F1, K1, 13, 1, x)
169 a, c = R(a, b, c, d, e, F1, K1, 11, 10, x)
170 e, b = R(e, a, b, c, d, F1, K1, 9, 6, x)
171 d, a = R(d, e, a, b, c, F1, K1, 7, 15, x)
172 c, e = R(c, d, e, a, b, F1, K1, 15, 3, x)
173 b, d = R(b, c, d, e, a, F1, K1, 7, 12, x)
174 a, c = R(a, b, c, d, e, F1, K1, 12, 0, x)
175 e, b = R(e, a, b, c, d, F1, K1, 15, 9, x)
176 d, a = R(d, e, a, b, c, F1, K1, 9, 5, x)
177 c, e = R(c, d, e, a, b, F1, K1, 11, 2, x)
178 b, d = R(b, c, d, e, a, F1, K1, 7, 14, x)
179 a, c = R(a, b, c, d, e, F1, K1, 13, 11, x)
180 e, b = R(e, a, b, c, d, F1, K1, 12, 8, x) # #31
181 # Round 3
182 d, a = R(d, e, a, b, c, F2, K2, 11, 3, x)
183 c, e = R(c, d, e, a, b, F2, K2, 13, 10, x)
184 b, d = R(b, c, d, e, a, F2, K2, 6, 14, x)
185 a, c = R(a, b, c, d, e, F2, K2, 7, 4, x)
186 e, b = R(e, a, b, c, d, F2, K2, 14, 9, x)
187 d, a = R(d, e, a, b, c, F2, K2, 9, 15, x)
188 c, e = R(c, d, e, a, b, F2, K2, 13, 8, x)
189 b, d = R(b, c, d, e, a, F2, K2, 15, 1, x)
190 a, c = R(a, b, c, d, e, F2, K2, 14, 2, x)
191 e, b = R(e, a, b, c, d, F2, K2, 8, 7, x)
192 d, a = R(d, e, a, b, c, F2, K2, 13, 0, x)
193 c, e = R(c, d, e, a, b, F2, K2, 6, 6, x)
194 b, d = R(b, c, d, e, a, F2, K2, 5, 13, x)
195 a, c = R(a, b, c, d, e, F2, K2, 12, 11, x)
196 e, b = R(e, a, b, c, d, F2, K2, 7, 5, x)
197 d, a = R(d, e, a, b, c, F2, K2, 5, 12, x) # #47
198 # Round 4
199 c, e = R(c, d, e, a, b, F3, K3, 11, 1, x)
200 b, d = R(b, c, d, e, a, F3, K3, 12, 9, x)
201 a, c = R(a, b, c, d, e, F3, K3, 14, 11, x)
202 e, b = R(e, a, b, c, d, F3, K3, 15, 10, x)
203 d, a = R(d, e, a, b, c, F3, K3, 14, 0, x)
204 c, e = R(c, d, e, a, b, F3, K3, 15, 8, x)
205 b, d = R(b, c, d, e, a, F3, K3, 9, 12, x)
206 a, c = R(a, b, c, d, e, F3, K3, 8, 4, x)
207 e, b = R(e, a, b, c, d, F3, K3, 9, 13, x)
208 d, a = R(d, e, a, b, c, F3, K3, 14, 3, x)
209 c, e = R(c, d, e, a, b, F3, K3, 5, 7, x)
210 b, d = R(b, c, d, e, a, F3, K3, 6, 15, x)
211 a, c = R(a, b, c, d, e, F3, K3, 8, 14, x)
212 e, b = R(e, a, b, c, d, F3, K3, 6, 5, x)
213 d, a = R(d, e, a, b, c, F3, K3, 5, 6, x)
214 c, e = R(c, d, e, a, b, F3, K3, 12, 2, x) # #63
215 # Round 5
216 b, d = R(b, c, d, e, a, F4, K4, 9, 4, x)
217 a, c = R(a, b, c, d, e, F4, K4, 15, 0, x)
218 e, b = R(e, a, b, c, d, F4, K4, 5, 5, x)
219 d, a = R(d, e, a, b, c, F4, K4, 11, 9, x)
220 c, e = R(c, d, e, a, b, F4, K4, 6, 7, x)
221 b, d = R(b, c, d, e, a, F4, K4, 8, 12, x)
222 a, c = R(a, b, c, d, e, F4, K4, 13, 2, x)
223 e, b = R(e, a, b, c, d, F4, K4, 12, 10, x)
224 d, a = R(d, e, a, b, c, F4, K4, 5, 14, x)
225 c, e = R(c, d, e, a, b, F4, K4, 12, 1, x)
226 b, d = R(b, c, d, e, a, F4, K4, 13, 3, x)
227 a, c = R(a, b, c, d, e, F4, K4, 14, 8, x)
228 e, b = R(e, a, b, c, d, F4, K4, 11, 11, x)
229 d, a = R(d, e, a, b, c, F4, K4, 8, 6, x)
230 c, e = R(c, d, e, a, b, F4, K4, 5, 15, x)
231 b, d = R(b, c, d, e, a, F4, K4, 6, 13, x) # #79
232
233 aa = a
234 bb = b
235 cc = c
236 dd = d
237 ee = e
238
239 a = state[0]
240 b = state[1]
241 c = state[2]
242 d = state[3]
243 e = state[4]
244
245 # Parallel round 1
246 a, c = R(a, b, c, d, e, F4, KK0, 8, 5, x)
247 e, b = R(e, a, b, c, d, F4, KK0, 9, 14, x)
248 d, a = R(d, e, a, b, c, F4, KK0, 9, 7, x)
249 c, e = R(c, d, e, a, b, F4, KK0, 11, 0, x)
250 b, d = R(b, c, d, e, a, F4, KK0, 13, 9, x)
251 a, c = R(a, b, c, d, e, F4, KK0, 15, 2, x)
252 e, b = R(e, a, b, c, d, F4, KK0, 15, 11, x)
253 d, a = R(d, e, a, b, c, F4, KK0, 5, 4, x)
254 c, e = R(c, d, e, a, b, F4, KK0, 7, 13, x)
255 b, d = R(b, c, d, e, a, F4, KK0, 7, 6, x)
256 a, c = R(a, b, c, d, e, F4, KK0, 8, 15, x)
257 e, b = R(e, a, b, c, d, F4, KK0, 11, 8, x)
258 d, a = R(d, e, a, b, c, F4, KK0, 14, 1, x)
259 c, e = R(c, d, e, a, b, F4, KK0, 14, 10, x)
260 b, d = R(b, c, d, e, a, F4, KK0, 12, 3, x)
261 a, c = R(a, b, c, d, e, F4, KK0, 6, 12, x) # #15
262 # Parallel round 2
263 e, b = R(e, a, b, c, d, F3, KK1, 9, 6, x)
264 d, a = R(d, e, a, b, c, F3, KK1, 13, 11, x)
265 c, e = R(c, d, e, a, b, F3, KK1, 15, 3, x)
266 b, d = R(b, c, d, e, a, F3, KK1, 7, 7, x)
267 a, c = R(a, b, c, d, e, F3, KK1, 12, 0, x)
268 e, b = R(e, a, b, c, d, F3, KK1, 8, 13, x)
269 d, a = R(d, e, a, b, c, F3, KK1, 9, 5, x)
270 c, e = R(c, d, e, a, b, F3, KK1, 11, 10, x)
271 b, d = R(b, c, d, e, a, F3, KK1, 7, 14, x)
272 a, c = R(a, b, c, d, e, F3, KK1, 7, 15, x)
273 e, b = R(e, a, b, c, d, F3, KK1, 12, 8, x)
274 d, a = R(d, e, a, b, c, F3, KK1, 7, 12, x)
275 c, e = R(c, d, e, a, b, F3, KK1, 6, 4, x)
276 b, d = R(b, c, d, e, a, F3, KK1, 15, 9, x)
277 a, c = R(a, b, c, d, e, F3, KK1, 13, 1, x)
278 e, b = R(e, a, b, c, d, F3, KK1, 11, 2, x) # #31
279 # Parallel round 3
280 d, a = R(d, e, a, b, c, F2, KK2, 9, 15, x)
281 c, e = R(c, d, e, a, b, F2, KK2, 7, 5, x)
282 b, d = R(b, c, d, e, a, F2, KK2, 15, 1, x)
283 a, c = R(a, b, c, d, e, F2, KK2, 11, 3, x)
284 e, b = R(e, a, b, c, d, F2, KK2, 8, 7, x)
285 d, a = R(d, e, a, b, c, F2, KK2, 6, 14, x)
286 c, e = R(c, d, e, a, b, F2, KK2, 6, 6, x)
287 b, d = R(b, c, d, e, a, F2, KK2, 14, 9, x)
288 a, c = R(a, b, c, d, e, F2, KK2, 12, 11, x)
289 e, b = R(e, a, b, c, d, F2, KK2, 13, 8, x)
290 d, a = R(d, e, a, b, c, F2, KK2, 5, 12, x)
291 c, e = R(c, d, e, a, b, F2, KK2, 14, 2, x)
292 b, d = R(b, c, d, e, a, F2, KK2, 13, 10, x)
293 a, c = R(a, b, c, d, e, F2, KK2, 13, 0, x)
294 e, b = R(e, a, b, c, d, F2, KK2, 7, 4, x)
295 d, a = R(d, e, a, b, c, F2, KK2, 5, 13, x) # #47
296 # Parallel round 4
297 c, e = R(c, d, e, a, b, F1, KK3, 15, 8, x)
298 b, d = R(b, c, d, e, a, F1, KK3, 5, 6, x)
299 a, c = R(a, b, c, d, e, F1, KK3, 8, 4, x)
300 e, b = R(e, a, b, c, d, F1, KK3, 11, 1, x)
301 d, a = R(d, e, a, b, c, F1, KK3, 14, 3, x)
302 c, e = R(c, d, e, a, b, F1, KK3, 14, 11, x)
303 b, d = R(b, c, d, e, a, F1, KK3, 6, 15, x)
304 a, c = R(a, b, c, d, e, F1, KK3, 14, 0, x)
305 e, b = R(e, a, b, c, d, F1, KK3, 6, 5, x)
306 d, a = R(d, e, a, b, c, F1, KK3, 9, 12, x)
307 c, e = R(c, d, e, a, b, F1, KK3, 12, 2, x)
308 b, d = R(b, c, d, e, a, F1, KK3, 9, 13, x)
309 a, c = R(a, b, c, d, e, F1, KK3, 12, 9, x)
310 e, b = R(e, a, b, c, d, F1, KK3, 5, 7, x)
311 d, a = R(d, e, a, b, c, F1, KK3, 15, 10, x)
312 c, e = R(c, d, e, a, b, F1, KK3, 8, 14, x) # #63
313 # Parallel round 5
314 b, d = R(b, c, d, e, a, F0, KK4, 8, 12, x)
315 a, c = R(a, b, c, d, e, F0, KK4, 5, 15, x)
316 e, b = R(e, a, b, c, d, F0, KK4, 12, 10, x)
317 d, a = R(d, e, a, b, c, F0, KK4, 9, 4, x)
318 c, e = R(c, d, e, a, b, F0, KK4, 12, 1, x)
319 b, d = R(b, c, d, e, a, F0, KK4, 5, 5, x)
320 a, c = R(a, b, c, d, e, F0, KK4, 14, 8, x)
321 e, b = R(e, a, b, c, d, F0, KK4, 6, 7, x)
322 d, a = R(d, e, a, b, c, F0, KK4, 8, 6, x)
323 c, e = R(c, d, e, a, b, F0, KK4, 13, 2, x)
324 b, d = R(b, c, d, e, a, F0, KK4, 6, 13, x)
325 a, c = R(a, b, c, d, e, F0, KK4, 5, 14, x)
326 e, b = R(e, a, b, c, d, F0, KK4, 15, 0, x)
327 d, a = R(d, e, a, b, c, F0, KK4, 13, 3, x)
328 c, e = R(c, d, e, a, b, F0, KK4, 11, 9, x)
329 b, d = R(b, c, d, e, a, F0, KK4, 11, 11, x) # #79
330
331 t = (state[1] + cc + d) % 0x100000000
332 state[1] = (state[2] + dd + e) % 0x100000000
333 state[2] = (state[3] + ee + a) % 0x100000000
334 state[3] = (state[4] + aa + b) % 0x100000000
335 state[4] = (state[0] + bb + c) % 0x100000000
336 state[0] = t % 0x100000000
337
338
339def RMD160Update(ctx, inp, inplen):
340 if type(inp) == str:
341 inp = [ord(i)&0xff for i in inp]
342
343 have = int((ctx.count // 8) % 64)
344 inplen = int(inplen)
345 need = 64 - have
346 ctx.count += 8 * inplen
347 off = 0
348 if inplen >= need:
349 if have:
350 for i in range(need):
351 ctx.buffer[have + i] = inp[i]
352 RMD160Transform(ctx.state, ctx.buffer)
353 off = need
354 have = 0
355 while off + 64 <= inplen:
356 RMD160Transform(ctx.state, inp[off:]) #<---
357 off += 64
358 if off < inplen:
359 # memcpy(ctx->buffer + have, input+off, len-off)
360 for i in range(inplen - off):
361 ctx.buffer[have + i] = inp[off + i]
362
363def RMD160Final(ctx):
364 size = struct.pack("<Q", ctx.count)
365 padlen = 64 - ((ctx.count // 8) % 64)
366 if padlen < 1 + 8:
367 padlen += 64
368 RMD160Update(ctx, PADDING, padlen - 8)
369 RMD160Update(ctx, size, 8)
370 return struct.pack("<5L", *ctx.state)
371
372
373assert "37f332f68db77bd9d7edd4969571ad671cf9dd3b" == new("The quick brown fox jumps over the lazy dog").hexdigest()
374assert "132072df690933835eb8b6ad0b77e7b6f14acad7" == new("The quick brown fox jumps over the lazy cog").hexdigest()
375assert "9c1185a5c5e9fc54612808977ee8f548b2258d31" == new("").hexdigest()