blob: daea887960062b1cfae3e27c8a2bb67093366719 [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
95#ifndef MIN
96 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
97#endif
98
99
100/* SHA1 macros */
101
102#define F0(x,y,z) (z ^ (x & (y ^ z)))
103#define F1(x,y,z) (x ^ y ^ z)
104#define F2(x,y,z) ((x & y) | (z & (x | y)))
105#define F3(x,y,z) (x ^ y ^ z)
106
107static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
108{
109 SHA1_INT32 a,b,c,d,e,W[80],i;
110
111 /* copy the state into 512-bits into W[0..15] */
112 for (i = 0; i < 16; i++) {
113 LOAD32H(W[i], buf + (4*i));
114 }
115
116 /* copy state */
117 a = sha1->state[0];
118 b = sha1->state[1];
119 c = sha1->state[2];
120 d = sha1->state[3];
121 e = sha1->state[4];
122
123 /* expand it */
124 for (i = 16; i < 80; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000126 }
127
128 /* compress */
129 /* round one */
Brett Cannonf079c572010-07-23 15:43:14 +0000130 #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);
131 #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);
132 #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);
133 #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 +0000134
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000135 for (i = 0; i < 20; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000136 FF_0(a,b,c,d,e,i++);
137 FF_0(e,a,b,c,d,i++);
138 FF_0(d,e,a,b,c,i++);
139 FF_0(c,d,e,a,b,i++);
140 FF_0(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000141 }
142
143 /* round two */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 for (; i < 40; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000145 FF_1(a,b,c,d,e,i++);
146 FF_1(e,a,b,c,d,i++);
147 FF_1(d,e,a,b,c,i++);
148 FF_1(c,d,e,a,b,i++);
149 FF_1(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000150 }
151
152 /* round three */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 for (; i < 60; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000154 FF_2(a,b,c,d,e,i++);
155 FF_2(e,a,b,c,d,i++);
156 FF_2(d,e,a,b,c,i++);
157 FF_2(c,d,e,a,b,i++);
158 FF_2(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000159 }
160
161 /* round four */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 for (; i < 80; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000163 FF_3(a,b,c,d,e,i++);
164 FF_3(e,a,b,c,d,i++);
165 FF_3(d,e,a,b,c,i++);
166 FF_3(c,d,e,a,b,i++);
167 FF_3(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000168 }
169
Brett Cannonf079c572010-07-23 15:43:14 +0000170 #undef FF_0
171 #undef FF_1
172 #undef FF_2
173 #undef FF_3
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000174
175 /* store */
176 sha1->state[0] = sha1->state[0] + a;
177 sha1->state[1] = sha1->state[1] + b;
178 sha1->state[2] = sha1->state[2] + c;
179 sha1->state[3] = sha1->state[3] + d;
180 sha1->state[4] = sha1->state[4] + e;
181}
182
183/**
184 Initialize the hash state
185 @param sha1 The hash state you wish to initialize
186*/
187void sha1_init(struct sha1_state *sha1)
188{
189 assert(sha1 != NULL);
190 sha1->state[0] = 0x67452301UL;
191 sha1->state[1] = 0xefcdab89UL;
192 sha1->state[2] = 0x98badcfeUL;
193 sha1->state[3] = 0x10325476UL;
194 sha1->state[4] = 0xc3d2e1f0UL;
195 sha1->curlen = 0;
196 sha1->length = 0;
197}
198
199/**
200 Process a block of memory though the hash
201 @param sha1 The hash state
202 @param in The data to hash
203 @param inlen The length of the data (octets)
204*/
205void sha1_process(struct sha1_state *sha1,
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000206 const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000207{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000208 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000209
210 assert(sha1 != NULL);
211 assert(in != NULL);
212 assert(sha1->curlen <= sizeof(sha1->buf));
213
214 while (inlen > 0) {
215 if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
216 sha1_compress(sha1, (unsigned char *)in);
217 sha1->length += SHA1_BLOCKSIZE * 8;
218 in += SHA1_BLOCKSIZE;
219 inlen -= SHA1_BLOCKSIZE;
220 } else {
Victor Stinner081fe462011-07-08 01:10:28 +0200221 n = MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000222 memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
223 sha1->curlen += n;
224 in += n;
225 inlen -= n;
226 if (sha1->curlen == SHA1_BLOCKSIZE) {
227 sha1_compress(sha1, sha1->buf);
228 sha1->length += 8*SHA1_BLOCKSIZE;
229 sha1->curlen = 0;
230 }
231 }
232 }
233}
234
235/**
236 Terminate the hash to get the digest
237 @param sha1 The hash state
238 @param out [out] The destination of the hash (20 bytes)
239*/
240void sha1_done(struct sha1_state *sha1, unsigned char *out)
241{
242 int i;
243
244 assert(sha1 != NULL);
245 assert(out != NULL);
246 assert(sha1->curlen < sizeof(sha1->buf));
247
248 /* increase the length of the message */
249 sha1->length += sha1->curlen * 8;
250
251 /* append the '1' bit */
252 sha1->buf[sha1->curlen++] = (unsigned char)0x80;
253
254 /* if the length is currently above 56 bytes we append zeros
255 * then compress. Then we can fall back to padding zeros and length
256 * encoding like normal.
257 */
258 if (sha1->curlen > 56) {
259 while (sha1->curlen < 64) {
260 sha1->buf[sha1->curlen++] = (unsigned char)0;
261 }
262 sha1_compress(sha1, sha1->buf);
263 sha1->curlen = 0;
264 }
265
266 /* pad upto 56 bytes of zeroes */
267 while (sha1->curlen < 56) {
268 sha1->buf[sha1->curlen++] = (unsigned char)0;
269 }
270
271 /* store length */
272 STORE64H(sha1->length, sha1->buf+56);
273 sha1_compress(sha1, sha1->buf);
274
275 /* copy output */
276 for (i = 0; i < 5; i++) {
277 STORE32H(sha1->state[i], out+(4*i));
278 }
279}
280
281
282/* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
283/* .Revision: 1.10 $ */
284/* .Date: 2007/05/12 14:25:28 $ */
285
286/*
287 * End of copied SHA1 code.
288 *
289 * ------------------------------------------------------------------------
290 */
291
292static PyTypeObject SHA1type;
293
294
295static SHA1object *
296newSHA1object(void)
297{
298 return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
299}
300
301
302/* Internal methods for a hash object */
303
304static void
305SHA1_dealloc(PyObject *ptr)
306{
307 PyObject_Del(ptr);
308}
309
310
311/* External methods for a hash object */
312
313PyDoc_STRVAR(SHA1_copy__doc__, "Return a copy of the hash object.");
314
315static PyObject *
316SHA1_copy(SHA1object *self, PyObject *unused)
317{
318 SHA1object *newobj;
319
Christian Heimes90aa7642007-12-19 02:45:37 +0000320 if (Py_TYPE(self) == &SHA1type) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000321 if ( (newobj = newSHA1object())==NULL)
322 return NULL;
323 } else {
324 if ( (newobj = newSHA1object())==NULL)
325 return NULL;
326 }
327
328 newobj->hash_state = self->hash_state;
329 return (PyObject *)newobj;
330}
331
332PyDoc_STRVAR(SHA1_digest__doc__,
333"Return the digest value as a string of binary data.");
334
335static PyObject *
336SHA1_digest(SHA1object *self, PyObject *unused)
337{
338 unsigned char digest[SHA1_DIGESTSIZE];
339 struct sha1_state temp;
340
341 temp = self->hash_state;
342 sha1_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000343 return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000344}
345
346PyDoc_STRVAR(SHA1_hexdigest__doc__,
347"Return the digest value as a string of hexadecimal digits.");
348
349static PyObject *
350SHA1_hexdigest(SHA1object *self, PyObject *unused)
351{
352 unsigned char digest[SHA1_DIGESTSIZE];
353 struct sha1_state temp;
354 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200355 Py_UCS1 *hex_digest;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000356 int i, j;
357
358 /* Get the raw (binary) digest value */
359 temp = self->hash_state;
360 sha1_done(&temp, digest);
361
362 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200363 retval = PyUnicode_New(SHA1_DIGESTSIZE * 2, 127);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000364 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200366 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000367
368 /* Make hex version of the digest */
369 for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200370 unsigned char c;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000371 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200372 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000373 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200374 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000375 }
Victor Stinner8f825062012-04-27 13:55:39 +0200376 assert(_PyUnicode_CheckConsistency(retval, 1));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000377 return retval;
378}
379
380PyDoc_STRVAR(SHA1_update__doc__,
381"Update this hash object's state with the provided string.");
382
383static PyObject *
384SHA1_update(SHA1object *self, PyObject *args)
385{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000386 PyObject *obj;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000387 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000388
Gregory P. Smith365a1862009-02-12 07:35:29 +0000389 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000390 return NULL;
391
Gregory P. Smith365a1862009-02-12 07:35:29 +0000392 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
393
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000394 sha1_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000395
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000396 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000397 Py_INCREF(Py_None);
398 return Py_None;
399}
400
401static PyMethodDef SHA1_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__},
403 {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__},
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000404 {"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS, SHA1_hexdigest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__},
406 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000407};
408
409static PyObject *
410SHA1_get_block_size(PyObject *self, void *closure)
411{
Christian Heimes217cfd12007-12-02 14:31:20 +0000412 return PyLong_FromLong(SHA1_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000413}
414
415static PyObject *
416SHA1_get_name(PyObject *self, void *closure)
417{
418 return PyUnicode_FromStringAndSize("SHA1", 3);
419}
420
421static PyObject *
422sha1_get_digest_size(PyObject *self, void *closure)
423{
Christian Heimes217cfd12007-12-02 14:31:20 +0000424 return PyLong_FromLong(SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000425}
426
427
428static PyGetSetDef SHA1_getseters[] = {
429 {"block_size",
430 (getter)SHA1_get_block_size, NULL,
431 NULL,
432 NULL},
433 {"name",
434 (getter)SHA1_get_name, NULL,
435 NULL,
436 NULL},
437 {"digest_size",
438 (getter)sha1_get_digest_size, NULL,
439 NULL,
440 NULL},
441 {NULL} /* Sentinel */
442};
443
444static PyTypeObject SHA1type = {
445 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 "_sha1.sha1", /*tp_name*/
447 sizeof(SHA1object), /*tp_size*/
448 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000449 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 SHA1_dealloc, /*tp_dealloc*/
451 0, /*tp_print*/
452 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000453 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000454 0, /*tp_reserved*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000455 0, /*tp_repr*/
456 0, /*tp_as_number*/
457 0, /*tp_as_sequence*/
458 0, /*tp_as_mapping*/
459 0, /*tp_hash*/
460 0, /*tp_call*/
461 0, /*tp_str*/
462 0, /*tp_getattro*/
463 0, /*tp_setattro*/
464 0, /*tp_as_buffer*/
465 Py_TPFLAGS_DEFAULT, /*tp_flags*/
466 0, /*tp_doc*/
467 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 0, /*tp_clear*/
469 0, /*tp_richcompare*/
470 0, /*tp_weaklistoffset*/
471 0, /*tp_iter*/
472 0, /*tp_iternext*/
473 SHA1_methods, /* tp_methods */
474 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000475 SHA1_getseters, /* tp_getset */
476};
477
478
479/* The single module-level function: new() */
480
481PyDoc_STRVAR(SHA1_new__doc__,
482"Return a new SHA1 hash object; optionally initialized with a string.");
483
484static PyObject *
485SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict)
486{
487 static char *kwlist[] = {"string", NULL};
488 SHA1object *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000489 PyObject *data_obj = NULL;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000490 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000491
Gregory P. Smith365a1862009-02-12 07:35:29 +0000492 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
493 &data_obj)) {
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000494 return NULL;
495 }
496
Gregory P. Smith365a1862009-02-12 07:35:29 +0000497 if (data_obj)
498 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
499
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000500 if ((new = newSHA1object()) == NULL) {
501 if (data_obj)
502 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000503 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000504 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000505
506 sha1_init(&new->hash_state);
507
508 if (PyErr_Occurred()) {
509 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000510 if (data_obj)
511 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000512 return NULL;
513 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000514 if (data_obj) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000515 sha1_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000516 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000517 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000518
519 return (PyObject *)new;
520}
521
522
523/* List of functions exported by this module */
524
525static struct PyMethodDef SHA1_functions[] = {
526 {"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000528};
529
530
531/* Initialize this module. */
532
533#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
534
Martin v. Löwis1a214512008-06-11 05:26:20 +0000535
536static struct PyModuleDef _sha1module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyModuleDef_HEAD_INIT,
538 "_sha1",
539 NULL,
540 -1,
541 SHA1_functions,
542 NULL,
543 NULL,
544 NULL,
545 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000546};
547
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000548PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000549PyInit__sha1(void)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000550{
Christian Heimes90aa7642007-12-19 02:45:37 +0000551 Py_TYPE(&SHA1type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000552 if (PyType_Ready(&SHA1type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000553 return NULL;
554 return PyModule_Create(&_sha1module);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000555}