blob: e4d9db40f22df36f8b78c4dfb3a758a2f5e6f0b9 [file] [log] [blame]
Gregory P. Smith2f21eb32007-09-09 06:44:34 +00001/* MD5 module */
2
3/* This module provides an interface to the MD5 algorithm */
4
5/* See below for information about the original code this module was
6 based upon. Additional work performed by:
7
8 Andrew Kuchling (amk@amk.ca)
9 Greg Stein (gstein@lyra.org)
10 Trevor Perrin (trevp@trevp.net)
11
12 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
13 Licensed to PSF under a Contributor Agreement.
14
15*/
16
17/* MD5 objects */
18
19#include "Python.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000020#include "hashlib.h"
Gregory P. Smith4dff6f62015-04-25 23:42:38 +000021#include "pystrhex.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000022
Martin v. Löwis501b13c2014-07-27 14:20:23 +020023/*[clinic input]
24module _md5
25class MD5Type "MD5object *" "&PyType_Type"
26[clinic start generated code]*/
27/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000028
29/* Some useful types */
30
31#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032typedef unsigned int MD5_INT32; /* 32-bit integer */
Benjamin Petersonaf580df2016-09-06 10:46:49 -070033typedef long long MD5_INT64; /* 64-bit integer */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000034#else
35/* not defined. compilation will die. */
36#endif
37
38/* The MD5 block size and message digest sizes, in bytes */
39
40#define MD5_BLOCKSIZE 64
41#define MD5_DIGESTSIZE 16
42
43/* The structure for storing MD5 info */
44
45struct md5_state {
46 MD5_INT64 length;
47 MD5_INT32 state[4], curlen;
48 unsigned char buf[MD5_BLOCKSIZE];
49};
50
51typedef struct {
52 PyObject_HEAD
53
54 struct md5_state hash_state;
55} MD5object;
56
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030057#include "clinic/md5module.c.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000058
59/* ------------------------------------------------------------------------
60 *
61 * This code for the MD5 algorithm was noted as public domain. The
62 * original headers are pasted below.
63 *
64 * Several changes have been made to make it more compatible with the
65 * Python environment and desired interface.
66 *
67 */
68
69/* LibTomCrypt, modular cryptographic library -- Tom St Denis
70 *
71 * LibTomCrypt is a library that provides various cryptographic
72 * algorithms in a highly modular and flexible manner.
73 *
74 * The library is free for all purposes without any express
75 * guarantee it works.
76 *
77 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
78 */
79
80/* rotate the hard way (platform optimizations could be done) */
81#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
82
83/* Endian Neutral macros that work on all platforms */
84
85#define STORE32L(x, y) \
86 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
87 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
88
89#define LOAD32L(x, y) \
90 { x = ((unsigned long)((y)[3] & 255)<<24) | \
91 ((unsigned long)((y)[2] & 255)<<16) | \
92 ((unsigned long)((y)[1] & 255)<<8) | \
93 ((unsigned long)((y)[0] & 255)); }
94
95#define STORE64L(x, y) \
96 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
97 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
98 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
99 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
100
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000101
102/* MD5 macros */
103
104#define F(x,y,z) (z ^ (x & (y ^ z)))
105#define G(x,y,z) (y ^ (z & (y ^ x)))
106#define H(x,y,z) (x^y^z)
107#define I(x,y,z) (y^(x|(~z)))
108
109#define FF(a,b,c,d,M,s,t) \
110 a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b;
111
112#define GG(a,b,c,d,M,s,t) \
113 a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b;
114
115#define HH(a,b,c,d,M,s,t) \
116 a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b;
117
118#define II(a,b,c,d,M,s,t) \
119 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
120
121
Andy Lester597ebed2020-02-12 22:53:01 -0600122static void md5_compress(struct md5_state *md5, const unsigned char *buf)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000123{
124 MD5_INT32 i, W[16], a, b, c, d;
125
126 assert(md5 != NULL);
127 assert(buf != NULL);
128
129 /* copy the state into 512-bits into W[0..15] */
130 for (i = 0; i < 16; i++) {
131 LOAD32L(W[i], buf + (4*i));
132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000134 /* copy state */
135 a = md5->state[0];
136 b = md5->state[1];
137 c = md5->state[2];
138 d = md5->state[3];
139
140 FF(a,b,c,d,W[0],7,0xd76aa478UL)
141 FF(d,a,b,c,W[1],12,0xe8c7b756UL)
142 FF(c,d,a,b,W[2],17,0x242070dbUL)
143 FF(b,c,d,a,W[3],22,0xc1bdceeeUL)
144 FF(a,b,c,d,W[4],7,0xf57c0fafUL)
145 FF(d,a,b,c,W[5],12,0x4787c62aUL)
146 FF(c,d,a,b,W[6],17,0xa8304613UL)
147 FF(b,c,d,a,W[7],22,0xfd469501UL)
148 FF(a,b,c,d,W[8],7,0x698098d8UL)
149 FF(d,a,b,c,W[9],12,0x8b44f7afUL)
150 FF(c,d,a,b,W[10],17,0xffff5bb1UL)
151 FF(b,c,d,a,W[11],22,0x895cd7beUL)
152 FF(a,b,c,d,W[12],7,0x6b901122UL)
153 FF(d,a,b,c,W[13],12,0xfd987193UL)
154 FF(c,d,a,b,W[14],17,0xa679438eUL)
155 FF(b,c,d,a,W[15],22,0x49b40821UL)
156 GG(a,b,c,d,W[1],5,0xf61e2562UL)
157 GG(d,a,b,c,W[6],9,0xc040b340UL)
158 GG(c,d,a,b,W[11],14,0x265e5a51UL)
159 GG(b,c,d,a,W[0],20,0xe9b6c7aaUL)
160 GG(a,b,c,d,W[5],5,0xd62f105dUL)
161 GG(d,a,b,c,W[10],9,0x02441453UL)
162 GG(c,d,a,b,W[15],14,0xd8a1e681UL)
163 GG(b,c,d,a,W[4],20,0xe7d3fbc8UL)
164 GG(a,b,c,d,W[9],5,0x21e1cde6UL)
165 GG(d,a,b,c,W[14],9,0xc33707d6UL)
166 GG(c,d,a,b,W[3],14,0xf4d50d87UL)
167 GG(b,c,d,a,W[8],20,0x455a14edUL)
168 GG(a,b,c,d,W[13],5,0xa9e3e905UL)
169 GG(d,a,b,c,W[2],9,0xfcefa3f8UL)
170 GG(c,d,a,b,W[7],14,0x676f02d9UL)
171 GG(b,c,d,a,W[12],20,0x8d2a4c8aUL)
172 HH(a,b,c,d,W[5],4,0xfffa3942UL)
173 HH(d,a,b,c,W[8],11,0x8771f681UL)
174 HH(c,d,a,b,W[11],16,0x6d9d6122UL)
175 HH(b,c,d,a,W[14],23,0xfde5380cUL)
176 HH(a,b,c,d,W[1],4,0xa4beea44UL)
177 HH(d,a,b,c,W[4],11,0x4bdecfa9UL)
178 HH(c,d,a,b,W[7],16,0xf6bb4b60UL)
179 HH(b,c,d,a,W[10],23,0xbebfbc70UL)
180 HH(a,b,c,d,W[13],4,0x289b7ec6UL)
181 HH(d,a,b,c,W[0],11,0xeaa127faUL)
182 HH(c,d,a,b,W[3],16,0xd4ef3085UL)
183 HH(b,c,d,a,W[6],23,0x04881d05UL)
184 HH(a,b,c,d,W[9],4,0xd9d4d039UL)
185 HH(d,a,b,c,W[12],11,0xe6db99e5UL)
186 HH(c,d,a,b,W[15],16,0x1fa27cf8UL)
187 HH(b,c,d,a,W[2],23,0xc4ac5665UL)
188 II(a,b,c,d,W[0],6,0xf4292244UL)
189 II(d,a,b,c,W[7],10,0x432aff97UL)
190 II(c,d,a,b,W[14],15,0xab9423a7UL)
191 II(b,c,d,a,W[5],21,0xfc93a039UL)
192 II(a,b,c,d,W[12],6,0x655b59c3UL)
193 II(d,a,b,c,W[3],10,0x8f0ccc92UL)
194 II(c,d,a,b,W[10],15,0xffeff47dUL)
195 II(b,c,d,a,W[1],21,0x85845dd1UL)
196 II(a,b,c,d,W[8],6,0x6fa87e4fUL)
197 II(d,a,b,c,W[15],10,0xfe2ce6e0UL)
198 II(c,d,a,b,W[6],15,0xa3014314UL)
199 II(b,c,d,a,W[13],21,0x4e0811a1UL)
200 II(a,b,c,d,W[4],6,0xf7537e82UL)
201 II(d,a,b,c,W[11],10,0xbd3af235UL)
202 II(c,d,a,b,W[2],15,0x2ad7d2bbUL)
203 II(b,c,d,a,W[9],21,0xeb86d391UL)
204
205 md5->state[0] = md5->state[0] + a;
206 md5->state[1] = md5->state[1] + b;
207 md5->state[2] = md5->state[2] + c;
208 md5->state[3] = md5->state[3] + d;
209}
210
211
212/**
213 Initialize the hash state
214 @param sha1 The hash state you wish to initialize
215*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200216static void
217md5_init(struct md5_state *md5)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000218{
219 assert(md5 != NULL);
220 md5->state[0] = 0x67452301UL;
221 md5->state[1] = 0xefcdab89UL;
222 md5->state[2] = 0x98badcfeUL;
223 md5->state[3] = 0x10325476UL;
224 md5->curlen = 0;
225 md5->length = 0;
226}
227
228/**
229 Process a block of memory though the hash
230 @param sha1 The hash state
231 @param in The data to hash
232 @param inlen The length of the data (octets)
233*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200234static void
235md5_process(struct md5_state *md5, const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000236{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000237 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000238
239 assert(md5 != NULL);
240 assert(in != NULL);
241 assert(md5->curlen <= sizeof(md5->buf));
242
243 while (inlen > 0) {
244 if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) {
Andy Lester597ebed2020-02-12 22:53:01 -0600245 md5_compress(md5, in);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000246 md5->length += MD5_BLOCKSIZE * 8;
247 in += MD5_BLOCKSIZE;
248 inlen -= MD5_BLOCKSIZE;
249 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200250 n = Py_MIN(inlen, (Py_ssize_t)(MD5_BLOCKSIZE - md5->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000251 memcpy(md5->buf + md5->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100252 md5->curlen += (MD5_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000253 in += n;
254 inlen -= n;
255 if (md5->curlen == MD5_BLOCKSIZE) {
256 md5_compress(md5, md5->buf);
257 md5->length += 8*MD5_BLOCKSIZE;
258 md5->curlen = 0;
259 }
260 }
261 }
262}
263
264/**
265 Terminate the hash to get the digest
266 @param sha1 The hash state
267 @param out [out] The destination of the hash (16 bytes)
268*/
doko@ubuntu.comc2b46732012-06-21 17:26:06 +0200269static void
270md5_done(struct md5_state *md5, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000271{
272 int i;
273
274 assert(md5 != NULL);
275 assert(out != NULL);
276 assert(md5->curlen < sizeof(md5->buf));
277
278 /* increase the length of the message */
279 md5->length += md5->curlen * 8;
280
281 /* append the '1' bit */
282 md5->buf[md5->curlen++] = (unsigned char)0x80;
283
284 /* if the length is currently above 56 bytes we append zeros
285 * then compress. Then we can fall back to padding zeros and length
286 * encoding like normal.
287 */
288 if (md5->curlen > 56) {
289 while (md5->curlen < 64) {
290 md5->buf[md5->curlen++] = (unsigned char)0;
291 }
292 md5_compress(md5, md5->buf);
293 md5->curlen = 0;
294 }
295
Leo Ariasc3d95082018-02-03 18:36:10 -0600296 /* pad up to 56 bytes of zeroes */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000297 while (md5->curlen < 56) {
298 md5->buf[md5->curlen++] = (unsigned char)0;
299 }
300
301 /* store length */
302 STORE64L(md5->length, md5->buf+56);
303 md5_compress(md5, md5->buf);
304
305 /* copy output */
306 for (i = 0; i < 4; i++) {
307 STORE32L(md5->state[i], out+(4*i));
308 }
309}
310
311/* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */
312/* .Revision: 1.10 $ */
313/* .Date: 2007/05/12 14:25:28 $ */
314
315/*
316 * End of copied MD5 code.
317 *
318 * ------------------------------------------------------------------------
319 */
320
321static PyTypeObject MD5type;
322
323
324static MD5object *
325newMD5object(void)
326{
327 return (MD5object *)PyObject_New(MD5object, &MD5type);
328}
329
330
331/* Internal methods for a hash object */
332
333static void
334MD5_dealloc(PyObject *ptr)
335{
336 PyObject_Del(ptr);
337}
338
339
340/* External methods for a hash object */
341
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200342/*[clinic input]
343MD5Type.copy
344
345Return a copy of the hash object.
346[clinic start generated code]*/
347
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200348static PyObject *
349MD5Type_copy_impl(MD5object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300350/*[clinic end generated code: output=596eb36852f02071 input=2c09e6d2493f3079]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000351{
352 MD5object *newobj;
353
Christian Heimes29fbd212015-04-16 17:29:11 +0200354 if ((newobj = newMD5object())==NULL)
355 return NULL;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000356
357 newobj->hash_state = self->hash_state;
358 return (PyObject *)newobj;
359}
360
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200361/*[clinic input]
362MD5Type.digest
363
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530364Return the digest value as a bytes object.
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200365[clinic start generated code]*/
366
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200367static PyObject *
368MD5Type_digest_impl(MD5object *self)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530369/*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000370{
371 unsigned char digest[MD5_DIGESTSIZE];
372 struct md5_state temp;
373
374 temp = self->hash_state;
375 md5_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000376 return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000377}
378
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200379/*[clinic input]
380MD5Type.hexdigest
381
382Return the digest value as a string of hexadecimal digits.
383[clinic start generated code]*/
384
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200385static PyObject *
386MD5Type_hexdigest_impl(MD5object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300387/*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000388{
389 unsigned char digest[MD5_DIGESTSIZE];
390 struct md5_state temp;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000391
392 /* Get the raw (binary) digest value */
393 temp = self->hash_state;
394 md5_done(&temp, digest);
395
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000396 return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000397}
398
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200399/*[clinic input]
400MD5Type.update
401
402 obj: object
403 /
404
405Update this hash object's state with the provided string.
406[clinic start generated code]*/
407
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000408static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200409MD5Type_update(MD5object *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300410/*[clinic end generated code: output=f6ad168416338423 input=6e1efcd9ecf17032]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000411{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000412 Py_buffer buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413
Gregory P. Smith365a1862009-02-12 07:35:29 +0000414 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
415
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000416 md5_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000417
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000418 PyBuffer_Release(&buf);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200419 Py_RETURN_NONE;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000420}
421
422static PyMethodDef MD5_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200423 MD5TYPE_COPY_METHODDEF
424 MD5TYPE_DIGEST_METHODDEF
425 MD5TYPE_HEXDIGEST_METHODDEF
426 MD5TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000428};
429
430static PyObject *
431MD5_get_block_size(PyObject *self, void *closure)
432{
Christian Heimes217cfd12007-12-02 14:31:20 +0000433 return PyLong_FromLong(MD5_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000434}
435
436static PyObject *
437MD5_get_name(PyObject *self, void *closure)
438{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200439 return PyUnicode_FromStringAndSize("md5", 3);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000440}
441
442static PyObject *
443md5_get_digest_size(PyObject *self, void *closure)
444{
Christian Heimes217cfd12007-12-02 14:31:20 +0000445 return PyLong_FromLong(MD5_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000446}
447
448
449static PyGetSetDef MD5_getseters[] = {
450 {"block_size",
451 (getter)MD5_get_block_size, NULL,
452 NULL,
453 NULL},
454 {"name",
455 (getter)MD5_get_name, NULL,
456 NULL,
457 NULL},
458 {"digest_size",
459 (getter)md5_get_digest_size, NULL,
460 NULL,
461 NULL},
462 {NULL} /* Sentinel */
463};
464
465static PyTypeObject MD5type = {
466 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 "_md5.md5", /*tp_name*/
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200468 sizeof(MD5object), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000470 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 MD5_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200472 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000474 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200475 0, /*tp_as_async*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000476 0, /*tp_repr*/
477 0, /*tp_as_number*/
478 0, /*tp_as_sequence*/
479 0, /*tp_as_mapping*/
480 0, /*tp_hash*/
481 0, /*tp_call*/
482 0, /*tp_str*/
483 0, /*tp_getattro*/
484 0, /*tp_setattro*/
485 0, /*tp_as_buffer*/
486 Py_TPFLAGS_DEFAULT, /*tp_flags*/
487 0, /*tp_doc*/
488 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 0, /*tp_clear*/
490 0, /*tp_richcompare*/
491 0, /*tp_weaklistoffset*/
492 0, /*tp_iter*/
493 0, /*tp_iternext*/
494 MD5_methods, /* tp_methods */
495 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000496 MD5_getseters, /* tp_getset */
497};
498
499
500/* The single module-level function: new() */
501
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200502/*[clinic input]
503_md5.md5
504
505 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200506 *
507 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200508
509Return a new MD5 hash object; optionally initialized with a string.
510[clinic start generated code]*/
511
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200512static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200513_md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity)
514/*[clinic end generated code: output=587071f76254a4ac input=7a144a1905636985]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200515{
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000516 MD5object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000517 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000518
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200519 if (string)
520 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000521
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000522 if ((new = newMD5object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200523 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000524 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000525 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000526 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000527
528 md5_init(&new->hash_state);
529
530 if (PyErr_Occurred()) {
531 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200532 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000533 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000534 return NULL;
535 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200536 if (string) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000537 md5_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000538 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000539 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000540
541 return (PyObject *)new;
542}
543
544
545/* List of functions exported by this module */
546
547static struct PyMethodDef MD5_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200548 _MD5_MD5_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000550};
551
552
553/* Initialize this module. */
554
Martin v. Löwis1a214512008-06-11 05:26:20 +0000555static struct PyModuleDef _md5module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyModuleDef_HEAD_INIT,
557 "_md5",
558 NULL,
559 -1,
560 MD5_functions,
561 NULL,
562 NULL,
563 NULL,
564 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000565};
566
567PyMODINIT_FUNC
568PyInit__md5(void)
569{
Christian Heimes327dd732013-10-22 15:05:23 +0200570 PyObject *m;
571
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100572 Py_SET_TYPE(&MD5type, &PyType_Type);
573 if (PyType_Ready(&MD5type) < 0) {
Martin v. Löwis1a214512008-06-11 05:26:20 +0000574 return NULL;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100575 }
Christian Heimes327dd732013-10-22 15:05:23 +0200576
577 m = PyModule_Create(&_md5module);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100578 if (m == NULL) {
Christian Heimes327dd732013-10-22 15:05:23 +0200579 return NULL;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100580 }
Christian Heimes327dd732013-10-22 15:05:23 +0200581
582 Py_INCREF((PyObject *)&MD5type);
583 PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type);
584 return m;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000585}