blob: b44fe189d267f54cf57cc9fb8db6e1c12650cf5d [file] [log] [blame]
Gregory P. Smith2f21eb32007-09-09 06:44:34 +00001/* SHA1 module */
2
3/* This module provides an interface to the SHA1 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/* SHA1 objects */
18
19#include "Python.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000020#include "hashlib.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000021
22
23/* Some useful types */
24
25#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026typedef unsigned int SHA1_INT32; /* 32-bit integer */
27typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000028#else
29/* not defined. compilation will die. */
30#endif
31
32/* The SHA1 block size and message digest sizes, in bytes */
33
34#define SHA1_BLOCKSIZE 64
35#define SHA1_DIGESTSIZE 20
36
37/* The structure for storing SHA1 info */
38
39struct sha1_state {
40 SHA1_INT64 length;
41 SHA1_INT32 state[5], curlen;
42 unsigned char buf[SHA1_BLOCKSIZE];
43};
44
45typedef struct {
46 PyObject_HEAD
47
48 struct sha1_state hash_state;
49} SHA1object;
50
51
52/* ------------------------------------------------------------------------
53 *
54 * This code for the SHA1 algorithm was noted as public domain. The
55 * original headers are pasted below.
56 *
57 * Several changes have been made to make it more compatible with the
58 * Python environment and desired interface.
59 *
60 */
61
62/* LibTomCrypt, modular cryptographic library -- Tom St Denis
63 *
64 * LibTomCrypt is a library that provides various cryptographic
65 * algorithms in a highly modular and flexible manner.
66 *
67 * The library is free for all purposes without any express
68 * guarantee it works.
69 *
70 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
71 */
72
73/* rotate the hard way (platform optimizations could be done) */
74#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
75#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
76
77/* Endian Neutral macros that work on all platforms */
78
79#define STORE32H(x, y) \
80 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
81 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
82
83#define LOAD32H(x, y) \
84 { x = ((unsigned long)((y)[0] & 255)<<24) | \
85 ((unsigned long)((y)[1] & 255)<<16) | \
86 ((unsigned long)((y)[2] & 255)<<8) | \
87 ((unsigned long)((y)[3] & 255)); }
88
89#define STORE64H(x, y) \
90 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
91 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
92 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
93 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
94
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000095
96/* SHA1 macros */
97
98#define F0(x,y,z) (z ^ (x & (y ^ z)))
99#define F1(x,y,z) (x ^ y ^ z)
100#define F2(x,y,z) ((x & y) | (z & (x | y)))
101#define F3(x,y,z) (x ^ y ^ z)
102
103static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
104{
105 SHA1_INT32 a,b,c,d,e,W[80],i;
106
107 /* copy the state into 512-bits into W[0..15] */
108 for (i = 0; i < 16; i++) {
109 LOAD32H(W[i], buf + (4*i));
110 }
111
112 /* copy state */
113 a = sha1->state[0];
114 b = sha1->state[1];
115 c = sha1->state[2];
116 d = sha1->state[3];
117 e = sha1->state[4];
118
119 /* expand it */
120 for (i = 16; i < 80; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000122 }
123
124 /* compress */
125 /* round one */
Brett Cannonf079c572010-07-23 15:43:14 +0000126 #define FF_0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
127 #define FF_1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
128 #define FF_2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
129 #define FF_3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000131 for (i = 0; i < 20; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000132 FF_0(a,b,c,d,e,i++);
133 FF_0(e,a,b,c,d,i++);
134 FF_0(d,e,a,b,c,i++);
135 FF_0(c,d,e,a,b,i++);
136 FF_0(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000137 }
138
139 /* round two */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 for (; i < 40; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000141 FF_1(a,b,c,d,e,i++);
142 FF_1(e,a,b,c,d,i++);
143 FF_1(d,e,a,b,c,i++);
144 FF_1(c,d,e,a,b,i++);
145 FF_1(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000146 }
147
148 /* round three */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 for (; i < 60; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000150 FF_2(a,b,c,d,e,i++);
151 FF_2(e,a,b,c,d,i++);
152 FF_2(d,e,a,b,c,i++);
153 FF_2(c,d,e,a,b,i++);
154 FF_2(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000155 }
156
157 /* round four */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 for (; i < 80; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000159 FF_3(a,b,c,d,e,i++);
160 FF_3(e,a,b,c,d,i++);
161 FF_3(d,e,a,b,c,i++);
162 FF_3(c,d,e,a,b,i++);
163 FF_3(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000164 }
165
Brett Cannonf079c572010-07-23 15:43:14 +0000166 #undef FF_0
167 #undef FF_1
168 #undef FF_2
169 #undef FF_3
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000170
171 /* store */
172 sha1->state[0] = sha1->state[0] + a;
173 sha1->state[1] = sha1->state[1] + b;
174 sha1->state[2] = sha1->state[2] + c;
175 sha1->state[3] = sha1->state[3] + d;
176 sha1->state[4] = sha1->state[4] + e;
177}
178
179/**
180 Initialize the hash state
181 @param sha1 The hash state you wish to initialize
182*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200183static void
184sha1_init(struct sha1_state *sha1)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000185{
186 assert(sha1 != NULL);
187 sha1->state[0] = 0x67452301UL;
188 sha1->state[1] = 0xefcdab89UL;
189 sha1->state[2] = 0x98badcfeUL;
190 sha1->state[3] = 0x10325476UL;
191 sha1->state[4] = 0xc3d2e1f0UL;
192 sha1->curlen = 0;
193 sha1->length = 0;
194}
195
196/**
197 Process a block of memory though the hash
198 @param sha1 The hash state
199 @param in The data to hash
200 @param inlen The length of the data (octets)
201*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200202static void
203sha1_process(struct sha1_state *sha1,
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000204 const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000205{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000206 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000207
208 assert(sha1 != NULL);
209 assert(in != NULL);
210 assert(sha1->curlen <= sizeof(sha1->buf));
211
212 while (inlen > 0) {
213 if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
214 sha1_compress(sha1, (unsigned char *)in);
215 sha1->length += SHA1_BLOCKSIZE * 8;
216 in += SHA1_BLOCKSIZE;
217 inlen -= SHA1_BLOCKSIZE;
218 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200219 n = Py_MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000220 memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100221 sha1->curlen += (SHA1_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000222 in += n;
223 inlen -= n;
224 if (sha1->curlen == SHA1_BLOCKSIZE) {
225 sha1_compress(sha1, sha1->buf);
226 sha1->length += 8*SHA1_BLOCKSIZE;
227 sha1->curlen = 0;
228 }
229 }
230 }
231}
232
233/**
234 Terminate the hash to get the digest
235 @param sha1 The hash state
236 @param out [out] The destination of the hash (20 bytes)
237*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200238static void
239sha1_done(struct sha1_state *sha1, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000240{
241 int i;
242
243 assert(sha1 != NULL);
244 assert(out != NULL);
245 assert(sha1->curlen < sizeof(sha1->buf));
246
247 /* increase the length of the message */
248 sha1->length += sha1->curlen * 8;
249
250 /* append the '1' bit */
251 sha1->buf[sha1->curlen++] = (unsigned char)0x80;
252
253 /* if the length is currently above 56 bytes we append zeros
254 * then compress. Then we can fall back to padding zeros and length
255 * encoding like normal.
256 */
257 if (sha1->curlen > 56) {
258 while (sha1->curlen < 64) {
259 sha1->buf[sha1->curlen++] = (unsigned char)0;
260 }
261 sha1_compress(sha1, sha1->buf);
262 sha1->curlen = 0;
263 }
264
265 /* pad upto 56 bytes of zeroes */
266 while (sha1->curlen < 56) {
267 sha1->buf[sha1->curlen++] = (unsigned char)0;
268 }
269
270 /* store length */
271 STORE64H(sha1->length, sha1->buf+56);
272 sha1_compress(sha1, sha1->buf);
273
274 /* copy output */
275 for (i = 0; i < 5; i++) {
276 STORE32H(sha1->state[i], out+(4*i));
277 }
278}
279
280
281/* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
282/* .Revision: 1.10 $ */
283/* .Date: 2007/05/12 14:25:28 $ */
284
285/*
286 * End of copied SHA1 code.
287 *
288 * ------------------------------------------------------------------------
289 */
290
291static PyTypeObject SHA1type;
292
293
294static SHA1object *
295newSHA1object(void)
296{
297 return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
298}
299
300
301/* Internal methods for a hash object */
302
303static void
304SHA1_dealloc(PyObject *ptr)
305{
306 PyObject_Del(ptr);
307}
308
309
310/* External methods for a hash object */
311
312PyDoc_STRVAR(SHA1_copy__doc__, "Return a copy of the hash object.");
313
314static PyObject *
315SHA1_copy(SHA1object *self, PyObject *unused)
316{
317 SHA1object *newobj;
318
Benjamin Peterson51ce29c2013-04-15 21:38:25 -0400319 if ((newobj = newSHA1object()) == NULL)
320 return NULL;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000321
322 newobj->hash_state = self->hash_state;
323 return (PyObject *)newobj;
324}
325
326PyDoc_STRVAR(SHA1_digest__doc__,
327"Return the digest value as a string of binary data.");
328
329static PyObject *
330SHA1_digest(SHA1object *self, PyObject *unused)
331{
332 unsigned char digest[SHA1_DIGESTSIZE];
333 struct sha1_state temp;
334
335 temp = self->hash_state;
336 sha1_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000337 return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000338}
339
340PyDoc_STRVAR(SHA1_hexdigest__doc__,
341"Return the digest value as a string of hexadecimal digits.");
342
343static PyObject *
344SHA1_hexdigest(SHA1object *self, PyObject *unused)
345{
346 unsigned char digest[SHA1_DIGESTSIZE];
347 struct sha1_state temp;
348 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200349 Py_UCS1 *hex_digest;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000350 int i, j;
351
352 /* Get the raw (binary) digest value */
353 temp = self->hash_state;
354 sha1_done(&temp, digest);
355
356 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200357 retval = PyUnicode_New(SHA1_DIGESTSIZE * 2, 127);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000358 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200360 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000361
362 /* Make hex version of the digest */
363 for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200364 unsigned char c;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000365 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200366 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000367 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200368 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000369 }
Christian Heimesf402e922013-01-03 09:21:55 +0100370#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200371 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100372#endif
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000373 return retval;
374}
375
376PyDoc_STRVAR(SHA1_update__doc__,
377"Update this hash object's state with the provided string.");
378
379static PyObject *
380SHA1_update(SHA1object *self, PyObject *args)
381{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000382 PyObject *obj;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000383 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000384
Gregory P. Smith365a1862009-02-12 07:35:29 +0000385 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000386 return NULL;
387
Gregory P. Smith365a1862009-02-12 07:35:29 +0000388 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
389
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000390 sha1_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000391
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000392 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000393 Py_INCREF(Py_None);
394 return Py_None;
395}
396
397static PyMethodDef SHA1_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__},
399 {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__},
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000400 {"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS, SHA1_hexdigest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__},
402 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000403};
404
405static PyObject *
406SHA1_get_block_size(PyObject *self, void *closure)
407{
Christian Heimes217cfd12007-12-02 14:31:20 +0000408 return PyLong_FromLong(SHA1_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000409}
410
411static PyObject *
412SHA1_get_name(PyObject *self, void *closure)
413{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200414 return PyUnicode_FromStringAndSize("sha1", 4);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000415}
416
417static PyObject *
418sha1_get_digest_size(PyObject *self, void *closure)
419{
Christian Heimes217cfd12007-12-02 14:31:20 +0000420 return PyLong_FromLong(SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000421}
422
423
424static PyGetSetDef SHA1_getseters[] = {
425 {"block_size",
426 (getter)SHA1_get_block_size, NULL,
427 NULL,
428 NULL},
429 {"name",
430 (getter)SHA1_get_name, NULL,
431 NULL,
432 NULL},
433 {"digest_size",
434 (getter)sha1_get_digest_size, NULL,
435 NULL,
436 NULL},
437 {NULL} /* Sentinel */
438};
439
440static PyTypeObject SHA1type = {
441 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 "_sha1.sha1", /*tp_name*/
443 sizeof(SHA1object), /*tp_size*/
444 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000445 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 SHA1_dealloc, /*tp_dealloc*/
447 0, /*tp_print*/
448 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000449 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000450 0, /*tp_reserved*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000451 0, /*tp_repr*/
452 0, /*tp_as_number*/
453 0, /*tp_as_sequence*/
454 0, /*tp_as_mapping*/
455 0, /*tp_hash*/
456 0, /*tp_call*/
457 0, /*tp_str*/
458 0, /*tp_getattro*/
459 0, /*tp_setattro*/
460 0, /*tp_as_buffer*/
461 Py_TPFLAGS_DEFAULT, /*tp_flags*/
462 0, /*tp_doc*/
463 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 0, /*tp_clear*/
465 0, /*tp_richcompare*/
466 0, /*tp_weaklistoffset*/
467 0, /*tp_iter*/
468 0, /*tp_iternext*/
469 SHA1_methods, /* tp_methods */
470 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000471 SHA1_getseters, /* tp_getset */
472};
473
474
475/* The single module-level function: new() */
476
477PyDoc_STRVAR(SHA1_new__doc__,
478"Return a new SHA1 hash object; optionally initialized with a string.");
479
480static PyObject *
481SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict)
482{
483 static char *kwlist[] = {"string", NULL};
484 SHA1object *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000485 PyObject *data_obj = NULL;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000486 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000487
Gregory P. Smith365a1862009-02-12 07:35:29 +0000488 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
489 &data_obj)) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000490 return NULL;
491 }
492
Gregory P. Smith365a1862009-02-12 07:35:29 +0000493 if (data_obj)
494 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
495
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000496 if ((new = newSHA1object()) == NULL) {
497 if (data_obj)
498 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000499 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000500 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000501
502 sha1_init(&new->hash_state);
503
504 if (PyErr_Occurred()) {
505 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000506 if (data_obj)
507 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000508 return NULL;
509 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000510 if (data_obj) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000511 sha1_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000512 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000513 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000514
515 return (PyObject *)new;
516}
517
518
519/* List of functions exported by this module */
520
521static struct PyMethodDef SHA1_functions[] = {
522 {"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000524};
525
526
527/* Initialize this module. */
528
529#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
530
Martin v. Löwis1a214512008-06-11 05:26:20 +0000531
532static struct PyModuleDef _sha1module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyModuleDef_HEAD_INIT,
534 "_sha1",
535 NULL,
536 -1,
537 SHA1_functions,
538 NULL,
539 NULL,
540 NULL,
541 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000542};
543
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000544PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000545PyInit__sha1(void)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000546{
Christian Heimes327dd732013-10-22 15:05:23 +0200547 PyObject *m;
548
Christian Heimes90aa7642007-12-19 02:45:37 +0000549 Py_TYPE(&SHA1type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000550 if (PyType_Ready(&SHA1type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000551 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200552
553 m = PyModule_Create(&_sha1module);
554 if (m == NULL)
555 return NULL;
556
557 Py_INCREF((PyObject *)&SHA1type);
558 PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);
559 return m;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000560}