You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

700 lines
18 KiB

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