1 | n/a | /* MD5 module */ |
---|
2 | n/a | |
---|
3 | n/a | /* This module provides an interface to the MD5 algorithm */ |
---|
4 | n/a | |
---|
5 | n/a | /* See below for information about the original code this module was |
---|
6 | n/a | based upon. Additional work performed by: |
---|
7 | n/a | |
---|
8 | n/a | Andrew Kuchling (amk@amk.ca) |
---|
9 | n/a | Greg Stein (gstein@lyra.org) |
---|
10 | n/a | Trevor Perrin (trevp@trevp.net) |
---|
11 | n/a | |
---|
12 | n/a | Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) |
---|
13 | n/a | Licensed to PSF under a Contributor Agreement. |
---|
14 | n/a | |
---|
15 | n/a | */ |
---|
16 | n/a | |
---|
17 | n/a | /* MD5 objects */ |
---|
18 | n/a | |
---|
19 | n/a | #include "Python.h" |
---|
20 | n/a | #include "hashlib.h" |
---|
21 | n/a | #include "pystrhex.h" |
---|
22 | n/a | |
---|
23 | n/a | /*[clinic input] |
---|
24 | n/a | module _md5 |
---|
25 | n/a | class MD5Type "MD5object *" "&PyType_Type" |
---|
26 | n/a | [clinic start generated code]*/ |
---|
27 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/ |
---|
28 | n/a | |
---|
29 | n/a | /* Some useful types */ |
---|
30 | n/a | |
---|
31 | n/a | #if SIZEOF_INT == 4 |
---|
32 | n/a | typedef unsigned int MD5_INT32; /* 32-bit integer */ |
---|
33 | n/a | typedef long long MD5_INT64; /* 64-bit integer */ |
---|
34 | n/a | #else |
---|
35 | n/a | /* not defined. compilation will die. */ |
---|
36 | n/a | #endif |
---|
37 | n/a | |
---|
38 | n/a | /* The MD5 block size and message digest sizes, in bytes */ |
---|
39 | n/a | |
---|
40 | n/a | #define MD5_BLOCKSIZE 64 |
---|
41 | n/a | #define MD5_DIGESTSIZE 16 |
---|
42 | n/a | |
---|
43 | n/a | /* The structure for storing MD5 info */ |
---|
44 | n/a | |
---|
45 | n/a | struct md5_state { |
---|
46 | n/a | MD5_INT64 length; |
---|
47 | n/a | MD5_INT32 state[4], curlen; |
---|
48 | n/a | unsigned char buf[MD5_BLOCKSIZE]; |
---|
49 | n/a | }; |
---|
50 | n/a | |
---|
51 | n/a | typedef struct { |
---|
52 | n/a | PyObject_HEAD |
---|
53 | n/a | |
---|
54 | n/a | struct md5_state hash_state; |
---|
55 | n/a | } MD5object; |
---|
56 | n/a | |
---|
57 | n/a | #include "clinic/md5module.c.h" |
---|
58 | n/a | |
---|
59 | n/a | /* ------------------------------------------------------------------------ |
---|
60 | n/a | * |
---|
61 | n/a | * This code for the MD5 algorithm was noted as public domain. The |
---|
62 | n/a | * original headers are pasted below. |
---|
63 | n/a | * |
---|
64 | n/a | * Several changes have been made to make it more compatible with the |
---|
65 | n/a | * Python environment and desired interface. |
---|
66 | n/a | * |
---|
67 | n/a | */ |
---|
68 | n/a | |
---|
69 | n/a | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
---|
70 | n/a | * |
---|
71 | n/a | * LibTomCrypt is a library that provides various cryptographic |
---|
72 | n/a | * algorithms in a highly modular and flexible manner. |
---|
73 | n/a | * |
---|
74 | n/a | * The library is free for all purposes without any express |
---|
75 | n/a | * guarantee it works. |
---|
76 | n/a | * |
---|
77 | n/a | * Tom St Denis, tomstdenis@gmail.com, http://libtom.org |
---|
78 | n/a | */ |
---|
79 | n/a | |
---|
80 | n/a | /* rotate the hard way (platform optimizations could be done) */ |
---|
81 | n/a | #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
---|
82 | n/a | |
---|
83 | n/a | /* Endian Neutral macros that work on all platforms */ |
---|
84 | n/a | |
---|
85 | n/a | #define STORE32L(x, y) \ |
---|
86 | n/a | { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
---|
87 | n/a | (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
---|
88 | n/a | |
---|
89 | n/a | #define LOAD32L(x, y) \ |
---|
90 | n/a | { x = ((unsigned long)((y)[3] & 255)<<24) | \ |
---|
91 | n/a | ((unsigned long)((y)[2] & 255)<<16) | \ |
---|
92 | n/a | ((unsigned long)((y)[1] & 255)<<8) | \ |
---|
93 | n/a | ((unsigned long)((y)[0] & 255)); } |
---|
94 | n/a | |
---|
95 | n/a | #define STORE64L(x, y) \ |
---|
96 | n/a | { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ |
---|
97 | n/a | (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ |
---|
98 | n/a | (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
---|
99 | n/a | (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } |
---|
100 | n/a | |
---|
101 | n/a | |
---|
102 | n/a | /* MD5 macros */ |
---|
103 | n/a | |
---|
104 | n/a | #define F(x,y,z) (z ^ (x & (y ^ z))) |
---|
105 | n/a | #define G(x,y,z) (y ^ (z & (y ^ x))) |
---|
106 | n/a | #define H(x,y,z) (x^y^z) |
---|
107 | n/a | #define I(x,y,z) (y^(x|(~z))) |
---|
108 | n/a | |
---|
109 | n/a | #define FF(a,b,c,d,M,s,t) \ |
---|
110 | n/a | a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b; |
---|
111 | n/a | |
---|
112 | n/a | #define GG(a,b,c,d,M,s,t) \ |
---|
113 | n/a | a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b; |
---|
114 | n/a | |
---|
115 | n/a | #define HH(a,b,c,d,M,s,t) \ |
---|
116 | n/a | a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b; |
---|
117 | n/a | |
---|
118 | n/a | #define II(a,b,c,d,M,s,t) \ |
---|
119 | n/a | a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b; |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | static void md5_compress(struct md5_state *md5, unsigned char *buf) |
---|
123 | n/a | { |
---|
124 | n/a | MD5_INT32 i, W[16], a, b, c, d; |
---|
125 | n/a | |
---|
126 | n/a | assert(md5 != NULL); |
---|
127 | n/a | assert(buf != NULL); |
---|
128 | n/a | |
---|
129 | n/a | /* copy the state into 512-bits into W[0..15] */ |
---|
130 | n/a | for (i = 0; i < 16; i++) { |
---|
131 | n/a | LOAD32L(W[i], buf + (4*i)); |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | /* copy state */ |
---|
135 | n/a | a = md5->state[0]; |
---|
136 | n/a | b = md5->state[1]; |
---|
137 | n/a | c = md5->state[2]; |
---|
138 | n/a | d = md5->state[3]; |
---|
139 | n/a | |
---|
140 | n/a | FF(a,b,c,d,W[0],7,0xd76aa478UL) |
---|
141 | n/a | FF(d,a,b,c,W[1],12,0xe8c7b756UL) |
---|
142 | n/a | FF(c,d,a,b,W[2],17,0x242070dbUL) |
---|
143 | n/a | FF(b,c,d,a,W[3],22,0xc1bdceeeUL) |
---|
144 | n/a | FF(a,b,c,d,W[4],7,0xf57c0fafUL) |
---|
145 | n/a | FF(d,a,b,c,W[5],12,0x4787c62aUL) |
---|
146 | n/a | FF(c,d,a,b,W[6],17,0xa8304613UL) |
---|
147 | n/a | FF(b,c,d,a,W[7],22,0xfd469501UL) |
---|
148 | n/a | FF(a,b,c,d,W[8],7,0x698098d8UL) |
---|
149 | n/a | FF(d,a,b,c,W[9],12,0x8b44f7afUL) |
---|
150 | n/a | FF(c,d,a,b,W[10],17,0xffff5bb1UL) |
---|
151 | n/a | FF(b,c,d,a,W[11],22,0x895cd7beUL) |
---|
152 | n/a | FF(a,b,c,d,W[12],7,0x6b901122UL) |
---|
153 | n/a | FF(d,a,b,c,W[13],12,0xfd987193UL) |
---|
154 | n/a | FF(c,d,a,b,W[14],17,0xa679438eUL) |
---|
155 | n/a | FF(b,c,d,a,W[15],22,0x49b40821UL) |
---|
156 | n/a | GG(a,b,c,d,W[1],5,0xf61e2562UL) |
---|
157 | n/a | GG(d,a,b,c,W[6],9,0xc040b340UL) |
---|
158 | n/a | GG(c,d,a,b,W[11],14,0x265e5a51UL) |
---|
159 | n/a | GG(b,c,d,a,W[0],20,0xe9b6c7aaUL) |
---|
160 | n/a | GG(a,b,c,d,W[5],5,0xd62f105dUL) |
---|
161 | n/a | GG(d,a,b,c,W[10],9,0x02441453UL) |
---|
162 | n/a | GG(c,d,a,b,W[15],14,0xd8a1e681UL) |
---|
163 | n/a | GG(b,c,d,a,W[4],20,0xe7d3fbc8UL) |
---|
164 | n/a | GG(a,b,c,d,W[9],5,0x21e1cde6UL) |
---|
165 | n/a | GG(d,a,b,c,W[14],9,0xc33707d6UL) |
---|
166 | n/a | GG(c,d,a,b,W[3],14,0xf4d50d87UL) |
---|
167 | n/a | GG(b,c,d,a,W[8],20,0x455a14edUL) |
---|
168 | n/a | GG(a,b,c,d,W[13],5,0xa9e3e905UL) |
---|
169 | n/a | GG(d,a,b,c,W[2],9,0xfcefa3f8UL) |
---|
170 | n/a | GG(c,d,a,b,W[7],14,0x676f02d9UL) |
---|
171 | n/a | GG(b,c,d,a,W[12],20,0x8d2a4c8aUL) |
---|
172 | n/a | HH(a,b,c,d,W[5],4,0xfffa3942UL) |
---|
173 | n/a | HH(d,a,b,c,W[8],11,0x8771f681UL) |
---|
174 | n/a | HH(c,d,a,b,W[11],16,0x6d9d6122UL) |
---|
175 | n/a | HH(b,c,d,a,W[14],23,0xfde5380cUL) |
---|
176 | n/a | HH(a,b,c,d,W[1],4,0xa4beea44UL) |
---|
177 | n/a | HH(d,a,b,c,W[4],11,0x4bdecfa9UL) |
---|
178 | n/a | HH(c,d,a,b,W[7],16,0xf6bb4b60UL) |
---|
179 | n/a | HH(b,c,d,a,W[10],23,0xbebfbc70UL) |
---|
180 | n/a | HH(a,b,c,d,W[13],4,0x289b7ec6UL) |
---|
181 | n/a | HH(d,a,b,c,W[0],11,0xeaa127faUL) |
---|
182 | n/a | HH(c,d,a,b,W[3],16,0xd4ef3085UL) |
---|
183 | n/a | HH(b,c,d,a,W[6],23,0x04881d05UL) |
---|
184 | n/a | HH(a,b,c,d,W[9],4,0xd9d4d039UL) |
---|
185 | n/a | HH(d,a,b,c,W[12],11,0xe6db99e5UL) |
---|
186 | n/a | HH(c,d,a,b,W[15],16,0x1fa27cf8UL) |
---|
187 | n/a | HH(b,c,d,a,W[2],23,0xc4ac5665UL) |
---|
188 | n/a | II(a,b,c,d,W[0],6,0xf4292244UL) |
---|
189 | n/a | II(d,a,b,c,W[7],10,0x432aff97UL) |
---|
190 | n/a | II(c,d,a,b,W[14],15,0xab9423a7UL) |
---|
191 | n/a | II(b,c,d,a,W[5],21,0xfc93a039UL) |
---|
192 | n/a | II(a,b,c,d,W[12],6,0x655b59c3UL) |
---|
193 | n/a | II(d,a,b,c,W[3],10,0x8f0ccc92UL) |
---|
194 | n/a | II(c,d,a,b,W[10],15,0xffeff47dUL) |
---|
195 | n/a | II(b,c,d,a,W[1],21,0x85845dd1UL) |
---|
196 | n/a | II(a,b,c,d,W[8],6,0x6fa87e4fUL) |
---|
197 | n/a | II(d,a,b,c,W[15],10,0xfe2ce6e0UL) |
---|
198 | n/a | II(c,d,a,b,W[6],15,0xa3014314UL) |
---|
199 | n/a | II(b,c,d,a,W[13],21,0x4e0811a1UL) |
---|
200 | n/a | II(a,b,c,d,W[4],6,0xf7537e82UL) |
---|
201 | n/a | II(d,a,b,c,W[11],10,0xbd3af235UL) |
---|
202 | n/a | II(c,d,a,b,W[2],15,0x2ad7d2bbUL) |
---|
203 | n/a | II(b,c,d,a,W[9],21,0xeb86d391UL) |
---|
204 | n/a | |
---|
205 | n/a | md5->state[0] = md5->state[0] + a; |
---|
206 | n/a | md5->state[1] = md5->state[1] + b; |
---|
207 | n/a | md5->state[2] = md5->state[2] + c; |
---|
208 | n/a | md5->state[3] = md5->state[3] + d; |
---|
209 | n/a | } |
---|
210 | n/a | |
---|
211 | n/a | |
---|
212 | n/a | /** |
---|
213 | n/a | Initialize the hash state |
---|
214 | n/a | @param sha1 The hash state you wish to initialize |
---|
215 | n/a | */ |
---|
216 | n/a | static void |
---|
217 | n/a | md5_init(struct md5_state *md5) |
---|
218 | n/a | { |
---|
219 | n/a | assert(md5 != NULL); |
---|
220 | n/a | md5->state[0] = 0x67452301UL; |
---|
221 | n/a | md5->state[1] = 0xefcdab89UL; |
---|
222 | n/a | md5->state[2] = 0x98badcfeUL; |
---|
223 | n/a | md5->state[3] = 0x10325476UL; |
---|
224 | n/a | md5->curlen = 0; |
---|
225 | n/a | md5->length = 0; |
---|
226 | n/a | } |
---|
227 | n/a | |
---|
228 | n/a | /** |
---|
229 | n/a | Process a block of memory though the hash |
---|
230 | n/a | @param sha1 The hash state |
---|
231 | n/a | @param in The data to hash |
---|
232 | n/a | @param inlen The length of the data (octets) |
---|
233 | n/a | */ |
---|
234 | n/a | static void |
---|
235 | n/a | md5_process(struct md5_state *md5, const unsigned char *in, Py_ssize_t inlen) |
---|
236 | n/a | { |
---|
237 | n/a | Py_ssize_t n; |
---|
238 | n/a | |
---|
239 | n/a | assert(md5 != NULL); |
---|
240 | n/a | assert(in != NULL); |
---|
241 | n/a | assert(md5->curlen <= sizeof(md5->buf)); |
---|
242 | n/a | |
---|
243 | n/a | while (inlen > 0) { |
---|
244 | n/a | if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) { |
---|
245 | n/a | md5_compress(md5, (unsigned char *)in); |
---|
246 | n/a | md5->length += MD5_BLOCKSIZE * 8; |
---|
247 | n/a | in += MD5_BLOCKSIZE; |
---|
248 | n/a | inlen -= MD5_BLOCKSIZE; |
---|
249 | n/a | } else { |
---|
250 | n/a | n = Py_MIN(inlen, (Py_ssize_t)(MD5_BLOCKSIZE - md5->curlen)); |
---|
251 | n/a | memcpy(md5->buf + md5->curlen, in, (size_t)n); |
---|
252 | n/a | md5->curlen += (MD5_INT32)n; |
---|
253 | n/a | in += n; |
---|
254 | n/a | inlen -= n; |
---|
255 | n/a | if (md5->curlen == MD5_BLOCKSIZE) { |
---|
256 | n/a | md5_compress(md5, md5->buf); |
---|
257 | n/a | md5->length += 8*MD5_BLOCKSIZE; |
---|
258 | n/a | md5->curlen = 0; |
---|
259 | n/a | } |
---|
260 | n/a | } |
---|
261 | n/a | } |
---|
262 | n/a | } |
---|
263 | n/a | |
---|
264 | n/a | /** |
---|
265 | n/a | Terminate the hash to get the digest |
---|
266 | n/a | @param sha1 The hash state |
---|
267 | n/a | @param out [out] The destination of the hash (16 bytes) |
---|
268 | n/a | */ |
---|
269 | n/a | static void |
---|
270 | n/a | md5_done(struct md5_state *md5, unsigned char *out) |
---|
271 | n/a | { |
---|
272 | n/a | int i; |
---|
273 | n/a | |
---|
274 | n/a | assert(md5 != NULL); |
---|
275 | n/a | assert(out != NULL); |
---|
276 | n/a | assert(md5->curlen < sizeof(md5->buf)); |
---|
277 | n/a | |
---|
278 | n/a | /* increase the length of the message */ |
---|
279 | n/a | md5->length += md5->curlen * 8; |
---|
280 | n/a | |
---|
281 | n/a | /* append the '1' bit */ |
---|
282 | n/a | md5->buf[md5->curlen++] = (unsigned char)0x80; |
---|
283 | n/a | |
---|
284 | n/a | /* if the length is currently above 56 bytes we append zeros |
---|
285 | n/a | * then compress. Then we can fall back to padding zeros and length |
---|
286 | n/a | * encoding like normal. |
---|
287 | n/a | */ |
---|
288 | n/a | if (md5->curlen > 56) { |
---|
289 | n/a | while (md5->curlen < 64) { |
---|
290 | n/a | md5->buf[md5->curlen++] = (unsigned char)0; |
---|
291 | n/a | } |
---|
292 | n/a | md5_compress(md5, md5->buf); |
---|
293 | n/a | md5->curlen = 0; |
---|
294 | n/a | } |
---|
295 | n/a | |
---|
296 | n/a | /* pad upto 56 bytes of zeroes */ |
---|
297 | n/a | while (md5->curlen < 56) { |
---|
298 | n/a | md5->buf[md5->curlen++] = (unsigned char)0; |
---|
299 | n/a | } |
---|
300 | n/a | |
---|
301 | n/a | /* store length */ |
---|
302 | n/a | STORE64L(md5->length, md5->buf+56); |
---|
303 | n/a | md5_compress(md5, md5->buf); |
---|
304 | n/a | |
---|
305 | n/a | /* copy output */ |
---|
306 | n/a | for (i = 0; i < 4; i++) { |
---|
307 | n/a | STORE32L(md5->state[i], out+(4*i)); |
---|
308 | n/a | } |
---|
309 | n/a | } |
---|
310 | n/a | |
---|
311 | n/a | /* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */ |
---|
312 | n/a | /* .Revision: 1.10 $ */ |
---|
313 | n/a | /* .Date: 2007/05/12 14:25:28 $ */ |
---|
314 | n/a | |
---|
315 | n/a | /* |
---|
316 | n/a | * End of copied MD5 code. |
---|
317 | n/a | * |
---|
318 | n/a | * ------------------------------------------------------------------------ |
---|
319 | n/a | */ |
---|
320 | n/a | |
---|
321 | n/a | static PyTypeObject MD5type; |
---|
322 | n/a | |
---|
323 | n/a | |
---|
324 | n/a | static MD5object * |
---|
325 | n/a | newMD5object(void) |
---|
326 | n/a | { |
---|
327 | n/a | return (MD5object *)PyObject_New(MD5object, &MD5type); |
---|
328 | n/a | } |
---|
329 | n/a | |
---|
330 | n/a | |
---|
331 | n/a | /* Internal methods for a hash object */ |
---|
332 | n/a | |
---|
333 | n/a | static void |
---|
334 | n/a | MD5_dealloc(PyObject *ptr) |
---|
335 | n/a | { |
---|
336 | n/a | PyObject_Del(ptr); |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | |
---|
340 | n/a | /* External methods for a hash object */ |
---|
341 | n/a | |
---|
342 | n/a | /*[clinic input] |
---|
343 | n/a | MD5Type.copy |
---|
344 | n/a | |
---|
345 | n/a | Return a copy of the hash object. |
---|
346 | n/a | [clinic start generated code]*/ |
---|
347 | n/a | |
---|
348 | n/a | static PyObject * |
---|
349 | n/a | MD5Type_copy_impl(MD5object *self) |
---|
350 | n/a | /*[clinic end generated code: output=596eb36852f02071 input=2c09e6d2493f3079]*/ |
---|
351 | n/a | { |
---|
352 | n/a | MD5object *newobj; |
---|
353 | n/a | |
---|
354 | n/a | if ((newobj = newMD5object())==NULL) |
---|
355 | n/a | return NULL; |
---|
356 | n/a | |
---|
357 | n/a | newobj->hash_state = self->hash_state; |
---|
358 | n/a | return (PyObject *)newobj; |
---|
359 | n/a | } |
---|
360 | n/a | |
---|
361 | n/a | /*[clinic input] |
---|
362 | n/a | MD5Type.digest |
---|
363 | n/a | |
---|
364 | n/a | Return the digest value as a string of binary data. |
---|
365 | n/a | [clinic start generated code]*/ |
---|
366 | n/a | |
---|
367 | n/a | static PyObject * |
---|
368 | n/a | MD5Type_digest_impl(MD5object *self) |
---|
369 | n/a | /*[clinic end generated code: output=eb691dc4190a07ec input=7b96e65389412a34]*/ |
---|
370 | n/a | { |
---|
371 | n/a | unsigned char digest[MD5_DIGESTSIZE]; |
---|
372 | n/a | struct md5_state temp; |
---|
373 | n/a | |
---|
374 | n/a | temp = self->hash_state; |
---|
375 | n/a | md5_done(&temp, digest); |
---|
376 | n/a | return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); |
---|
377 | n/a | } |
---|
378 | n/a | |
---|
379 | n/a | /*[clinic input] |
---|
380 | n/a | MD5Type.hexdigest |
---|
381 | n/a | |
---|
382 | n/a | Return the digest value as a string of hexadecimal digits. |
---|
383 | n/a | [clinic start generated code]*/ |
---|
384 | n/a | |
---|
385 | n/a | static PyObject * |
---|
386 | n/a | MD5Type_hexdigest_impl(MD5object *self) |
---|
387 | n/a | /*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/ |
---|
388 | n/a | { |
---|
389 | n/a | unsigned char digest[MD5_DIGESTSIZE]; |
---|
390 | n/a | struct md5_state temp; |
---|
391 | n/a | |
---|
392 | n/a | /* Get the raw (binary) digest value */ |
---|
393 | n/a | temp = self->hash_state; |
---|
394 | n/a | md5_done(&temp, digest); |
---|
395 | n/a | |
---|
396 | n/a | return _Py_strhex((const char*)digest, MD5_DIGESTSIZE); |
---|
397 | n/a | } |
---|
398 | n/a | |
---|
399 | n/a | /*[clinic input] |
---|
400 | n/a | MD5Type.update |
---|
401 | n/a | |
---|
402 | n/a | obj: object |
---|
403 | n/a | / |
---|
404 | n/a | |
---|
405 | n/a | Update this hash object's state with the provided string. |
---|
406 | n/a | [clinic start generated code]*/ |
---|
407 | n/a | |
---|
408 | n/a | static PyObject * |
---|
409 | n/a | MD5Type_update(MD5object *self, PyObject *obj) |
---|
410 | n/a | /*[clinic end generated code: output=f6ad168416338423 input=6e1efcd9ecf17032]*/ |
---|
411 | n/a | { |
---|
412 | n/a | Py_buffer buf; |
---|
413 | n/a | |
---|
414 | n/a | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
---|
415 | n/a | |
---|
416 | n/a | md5_process(&self->hash_state, buf.buf, buf.len); |
---|
417 | n/a | |
---|
418 | n/a | PyBuffer_Release(&buf); |
---|
419 | n/a | Py_RETURN_NONE; |
---|
420 | n/a | } |
---|
421 | n/a | |
---|
422 | n/a | static PyMethodDef MD5_methods[] = { |
---|
423 | n/a | MD5TYPE_COPY_METHODDEF |
---|
424 | n/a | MD5TYPE_DIGEST_METHODDEF |
---|
425 | n/a | MD5TYPE_HEXDIGEST_METHODDEF |
---|
426 | n/a | MD5TYPE_UPDATE_METHODDEF |
---|
427 | n/a | {NULL, NULL} /* sentinel */ |
---|
428 | n/a | }; |
---|
429 | n/a | |
---|
430 | n/a | static PyObject * |
---|
431 | n/a | MD5_get_block_size(PyObject *self, void *closure) |
---|
432 | n/a | { |
---|
433 | n/a | return PyLong_FromLong(MD5_BLOCKSIZE); |
---|
434 | n/a | } |
---|
435 | n/a | |
---|
436 | n/a | static PyObject * |
---|
437 | n/a | MD5_get_name(PyObject *self, void *closure) |
---|
438 | n/a | { |
---|
439 | n/a | return PyUnicode_FromStringAndSize("md5", 3); |
---|
440 | n/a | } |
---|
441 | n/a | |
---|
442 | n/a | static PyObject * |
---|
443 | n/a | md5_get_digest_size(PyObject *self, void *closure) |
---|
444 | n/a | { |
---|
445 | n/a | return PyLong_FromLong(MD5_DIGESTSIZE); |
---|
446 | n/a | } |
---|
447 | n/a | |
---|
448 | n/a | |
---|
449 | n/a | static PyGetSetDef MD5_getseters[] = { |
---|
450 | n/a | {"block_size", |
---|
451 | n/a | (getter)MD5_get_block_size, NULL, |
---|
452 | n/a | NULL, |
---|
453 | n/a | NULL}, |
---|
454 | n/a | {"name", |
---|
455 | n/a | (getter)MD5_get_name, NULL, |
---|
456 | n/a | NULL, |
---|
457 | n/a | NULL}, |
---|
458 | n/a | {"digest_size", |
---|
459 | n/a | (getter)md5_get_digest_size, NULL, |
---|
460 | n/a | NULL, |
---|
461 | n/a | NULL}, |
---|
462 | n/a | {NULL} /* Sentinel */ |
---|
463 | n/a | }; |
---|
464 | n/a | |
---|
465 | n/a | static PyTypeObject MD5type = { |
---|
466 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
467 | n/a | "_md5.md5", /*tp_name*/ |
---|
468 | n/a | sizeof(MD5object), /*tp_size*/ |
---|
469 | n/a | 0, /*tp_itemsize*/ |
---|
470 | n/a | /* methods */ |
---|
471 | n/a | MD5_dealloc, /*tp_dealloc*/ |
---|
472 | n/a | 0, /*tp_print*/ |
---|
473 | n/a | 0, /*tp_getattr*/ |
---|
474 | n/a | 0, /*tp_setattr*/ |
---|
475 | n/a | 0, /*tp_reserved*/ |
---|
476 | n/a | 0, /*tp_repr*/ |
---|
477 | n/a | 0, /*tp_as_number*/ |
---|
478 | n/a | 0, /*tp_as_sequence*/ |
---|
479 | n/a | 0, /*tp_as_mapping*/ |
---|
480 | n/a | 0, /*tp_hash*/ |
---|
481 | n/a | 0, /*tp_call*/ |
---|
482 | n/a | 0, /*tp_str*/ |
---|
483 | n/a | 0, /*tp_getattro*/ |
---|
484 | n/a | 0, /*tp_setattro*/ |
---|
485 | n/a | 0, /*tp_as_buffer*/ |
---|
486 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
487 | n/a | 0, /*tp_doc*/ |
---|
488 | n/a | 0, /*tp_traverse*/ |
---|
489 | n/a | 0, /*tp_clear*/ |
---|
490 | n/a | 0, /*tp_richcompare*/ |
---|
491 | n/a | 0, /*tp_weaklistoffset*/ |
---|
492 | n/a | 0, /*tp_iter*/ |
---|
493 | n/a | 0, /*tp_iternext*/ |
---|
494 | n/a | MD5_methods, /* tp_methods */ |
---|
495 | n/a | NULL, /* tp_members */ |
---|
496 | n/a | MD5_getseters, /* tp_getset */ |
---|
497 | n/a | }; |
---|
498 | n/a | |
---|
499 | n/a | |
---|
500 | n/a | /* The single module-level function: new() */ |
---|
501 | n/a | |
---|
502 | n/a | /*[clinic input] |
---|
503 | n/a | _md5.md5 |
---|
504 | n/a | |
---|
505 | n/a | string: object(c_default="NULL") = b'' |
---|
506 | n/a | |
---|
507 | n/a | Return a new MD5 hash object; optionally initialized with a string. |
---|
508 | n/a | [clinic start generated code]*/ |
---|
509 | n/a | |
---|
510 | n/a | static PyObject * |
---|
511 | n/a | _md5_md5_impl(PyObject *module, PyObject *string) |
---|
512 | n/a | /*[clinic end generated code: output=2cfd0f8c091b97e6 input=d12ef8f72d684f7b]*/ |
---|
513 | n/a | { |
---|
514 | n/a | MD5object *new; |
---|
515 | n/a | Py_buffer buf; |
---|
516 | n/a | |
---|
517 | n/a | if (string) |
---|
518 | n/a | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
---|
519 | n/a | |
---|
520 | n/a | if ((new = newMD5object()) == NULL) { |
---|
521 | n/a | if (string) |
---|
522 | n/a | PyBuffer_Release(&buf); |
---|
523 | n/a | return NULL; |
---|
524 | n/a | } |
---|
525 | n/a | |
---|
526 | n/a | md5_init(&new->hash_state); |
---|
527 | n/a | |
---|
528 | n/a | if (PyErr_Occurred()) { |
---|
529 | n/a | Py_DECREF(new); |
---|
530 | n/a | if (string) |
---|
531 | n/a | PyBuffer_Release(&buf); |
---|
532 | n/a | return NULL; |
---|
533 | n/a | } |
---|
534 | n/a | if (string) { |
---|
535 | n/a | md5_process(&new->hash_state, buf.buf, buf.len); |
---|
536 | n/a | PyBuffer_Release(&buf); |
---|
537 | n/a | } |
---|
538 | n/a | |
---|
539 | n/a | return (PyObject *)new; |
---|
540 | n/a | } |
---|
541 | n/a | |
---|
542 | n/a | |
---|
543 | n/a | /* List of functions exported by this module */ |
---|
544 | n/a | |
---|
545 | n/a | static struct PyMethodDef MD5_functions[] = { |
---|
546 | n/a | _MD5_MD5_METHODDEF |
---|
547 | n/a | {NULL, NULL} /* Sentinel */ |
---|
548 | n/a | }; |
---|
549 | n/a | |
---|
550 | n/a | |
---|
551 | n/a | /* Initialize this module. */ |
---|
552 | n/a | |
---|
553 | n/a | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
---|
554 | n/a | |
---|
555 | n/a | |
---|
556 | n/a | static struct PyModuleDef _md5module = { |
---|
557 | n/a | PyModuleDef_HEAD_INIT, |
---|
558 | n/a | "_md5", |
---|
559 | n/a | NULL, |
---|
560 | n/a | -1, |
---|
561 | n/a | MD5_functions, |
---|
562 | n/a | NULL, |
---|
563 | n/a | NULL, |
---|
564 | n/a | NULL, |
---|
565 | n/a | NULL |
---|
566 | n/a | }; |
---|
567 | n/a | |
---|
568 | n/a | PyMODINIT_FUNC |
---|
569 | n/a | PyInit__md5(void) |
---|
570 | n/a | { |
---|
571 | n/a | PyObject *m; |
---|
572 | n/a | |
---|
573 | n/a | Py_TYPE(&MD5type) = &PyType_Type; |
---|
574 | n/a | if (PyType_Ready(&MD5type) < 0) |
---|
575 | n/a | return NULL; |
---|
576 | n/a | |
---|
577 | n/a | m = PyModule_Create(&_md5module); |
---|
578 | n/a | if (m == NULL) |
---|
579 | n/a | return NULL; |
---|
580 | n/a | |
---|
581 | n/a | Py_INCREF((PyObject *)&MD5type); |
---|
582 | n/a | PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type); |
---|
583 | n/a | return m; |
---|
584 | n/a | } |
---|