blob: d1f89364d7b0734723f0c4e9038a4279cd7d2b48 [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
Martin v. Löwis501b13c2014-07-27 14:20:23 +020022/*[clinic input]
23module _sha1
24class SHA1Type "SHA1object *" "&PyType_Type"
25[clinic start generated code]*/
26/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000027
28/* Some useful types */
29
30#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031typedef unsigned int SHA1_INT32; /* 32-bit integer */
32typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000033#else
34/* not defined. compilation will die. */
35#endif
36
37/* The SHA1 block size and message digest sizes, in bytes */
38
39#define SHA1_BLOCKSIZE 64
40#define SHA1_DIGESTSIZE 20
41
42/* The structure for storing SHA1 info */
43
44struct sha1_state {
45 SHA1_INT64 length;
46 SHA1_INT32 state[5], curlen;
47 unsigned char buf[SHA1_BLOCKSIZE];
48};
49
50typedef struct {
51 PyObject_HEAD
52
53 struct sha1_state hash_state;
54} SHA1object;
55
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030056#include "clinic/sha1module.c.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000057
58/* ------------------------------------------------------------------------
59 *
60 * This code for the SHA1 algorithm was noted as public domain. The
61 * original headers are pasted below.
62 *
63 * Several changes have been made to make it more compatible with the
64 * Python environment and desired interface.
65 *
66 */
67
68/* LibTomCrypt, modular cryptographic library -- Tom St Denis
69 *
70 * LibTomCrypt is a library that provides various cryptographic
71 * algorithms in a highly modular and flexible manner.
72 *
73 * The library is free for all purposes without any express
74 * guarantee it works.
75 *
76 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
77 */
78
79/* rotate the hard way (platform optimizations could be done) */
80#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
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 STORE32H(x, y) \
86 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
87 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
88
89#define LOAD32H(x, y) \
90 { x = ((unsigned long)((y)[0] & 255)<<24) | \
91 ((unsigned long)((y)[1] & 255)<<16) | \
92 ((unsigned long)((y)[2] & 255)<<8) | \
93 ((unsigned long)((y)[3] & 255)); }
94
95#define STORE64H(x, y) \
96 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
97 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
98 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
99 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
100
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000101
102/* SHA1 macros */
103
104#define F0(x,y,z) (z ^ (x & (y ^ z)))
105#define F1(x,y,z) (x ^ y ^ z)
106#define F2(x,y,z) ((x & y) | (z & (x | y)))
107#define F3(x,y,z) (x ^ y ^ z)
108
109static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
110{
111 SHA1_INT32 a,b,c,d,e,W[80],i;
112
113 /* copy the state into 512-bits into W[0..15] */
114 for (i = 0; i < 16; i++) {
115 LOAD32H(W[i], buf + (4*i));
116 }
117
118 /* copy state */
119 a = sha1->state[0];
120 b = sha1->state[1];
121 c = sha1->state[2];
122 d = sha1->state[3];
123 e = sha1->state[4];
124
125 /* expand it */
126 for (i = 16; i < 80; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000128 }
129
130 /* compress */
131 /* round one */
Brett Cannonf079c572010-07-23 15:43:14 +0000132 #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);
133 #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);
134 #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);
135 #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 +0000136
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000137 for (i = 0; i < 20; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000138 FF_0(a,b,c,d,e,i++);
139 FF_0(e,a,b,c,d,i++);
140 FF_0(d,e,a,b,c,i++);
141 FF_0(c,d,e,a,b,i++);
142 FF_0(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000143 }
144
145 /* round two */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 for (; i < 40; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000147 FF_1(a,b,c,d,e,i++);
148 FF_1(e,a,b,c,d,i++);
149 FF_1(d,e,a,b,c,i++);
150 FF_1(c,d,e,a,b,i++);
151 FF_1(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000152 }
153
154 /* round three */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 for (; i < 60; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000156 FF_2(a,b,c,d,e,i++);
157 FF_2(e,a,b,c,d,i++);
158 FF_2(d,e,a,b,c,i++);
159 FF_2(c,d,e,a,b,i++);
160 FF_2(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000161 }
162
163 /* round four */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 for (; i < 80; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000165 FF_3(a,b,c,d,e,i++);
166 FF_3(e,a,b,c,d,i++);
167 FF_3(d,e,a,b,c,i++);
168 FF_3(c,d,e,a,b,i++);
169 FF_3(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000170 }
171
Brett Cannonf079c572010-07-23 15:43:14 +0000172 #undef FF_0
173 #undef FF_1
174 #undef FF_2
175 #undef FF_3
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000176
177 /* store */
178 sha1->state[0] = sha1->state[0] + a;
179 sha1->state[1] = sha1->state[1] + b;
180 sha1->state[2] = sha1->state[2] + c;
181 sha1->state[3] = sha1->state[3] + d;
182 sha1->state[4] = sha1->state[4] + e;
183}
184
185/**
186 Initialize the hash state
187 @param sha1 The hash state you wish to initialize
188*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200189static void
190sha1_init(struct sha1_state *sha1)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000191{
192 assert(sha1 != NULL);
193 sha1->state[0] = 0x67452301UL;
194 sha1->state[1] = 0xefcdab89UL;
195 sha1->state[2] = 0x98badcfeUL;
196 sha1->state[3] = 0x10325476UL;
197 sha1->state[4] = 0xc3d2e1f0UL;
198 sha1->curlen = 0;
199 sha1->length = 0;
200}
201
202/**
203 Process a block of memory though the hash
204 @param sha1 The hash state
205 @param in The data to hash
206 @param inlen The length of the data (octets)
207*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200208static void
209sha1_process(struct sha1_state *sha1,
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000210 const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000211{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000212 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000213
214 assert(sha1 != NULL);
215 assert(in != NULL);
216 assert(sha1->curlen <= sizeof(sha1->buf));
217
218 while (inlen > 0) {
219 if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
220 sha1_compress(sha1, (unsigned char *)in);
221 sha1->length += SHA1_BLOCKSIZE * 8;
222 in += SHA1_BLOCKSIZE;
223 inlen -= SHA1_BLOCKSIZE;
224 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200225 n = Py_MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000226 memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100227 sha1->curlen += (SHA1_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000228 in += n;
229 inlen -= n;
230 if (sha1->curlen == SHA1_BLOCKSIZE) {
231 sha1_compress(sha1, sha1->buf);
232 sha1->length += 8*SHA1_BLOCKSIZE;
233 sha1->curlen = 0;
234 }
235 }
236 }
237}
238
239/**
240 Terminate the hash to get the digest
241 @param sha1 The hash state
242 @param out [out] The destination of the hash (20 bytes)
243*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200244static void
245sha1_done(struct sha1_state *sha1, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000246{
247 int i;
248
249 assert(sha1 != NULL);
250 assert(out != NULL);
251 assert(sha1->curlen < sizeof(sha1->buf));
252
253 /* increase the length of the message */
254 sha1->length += sha1->curlen * 8;
255
256 /* append the '1' bit */
257 sha1->buf[sha1->curlen++] = (unsigned char)0x80;
258
259 /* if the length is currently above 56 bytes we append zeros
260 * then compress. Then we can fall back to padding zeros and length
261 * encoding like normal.
262 */
263 if (sha1->curlen > 56) {
264 while (sha1->curlen < 64) {
265 sha1->buf[sha1->curlen++] = (unsigned char)0;
266 }
267 sha1_compress(sha1, sha1->buf);
268 sha1->curlen = 0;
269 }
270
271 /* pad upto 56 bytes of zeroes */
272 while (sha1->curlen < 56) {
273 sha1->buf[sha1->curlen++] = (unsigned char)0;
274 }
275
276 /* store length */
277 STORE64H(sha1->length, sha1->buf+56);
278 sha1_compress(sha1, sha1->buf);
279
280 /* copy output */
281 for (i = 0; i < 5; i++) {
282 STORE32H(sha1->state[i], out+(4*i));
283 }
284}
285
286
287/* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
288/* .Revision: 1.10 $ */
289/* .Date: 2007/05/12 14:25:28 $ */
290
291/*
292 * End of copied SHA1 code.
293 *
294 * ------------------------------------------------------------------------
295 */
296
297static PyTypeObject SHA1type;
298
299
300static SHA1object *
301newSHA1object(void)
302{
303 return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
304}
305
306
307/* Internal methods for a hash object */
308
309static void
310SHA1_dealloc(PyObject *ptr)
311{
312 PyObject_Del(ptr);
313}
314
315
316/* External methods for a hash object */
317
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200318/*[clinic input]
319SHA1Type.copy
320
321Return a copy of the hash object.
322[clinic start generated code]*/
323
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200324static PyObject *
325SHA1Type_copy_impl(SHA1object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300326/*[clinic end generated code: output=b4e001264620f02a input=b7eae10df6f89b36]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000327{
328 SHA1object *newobj;
329
Benjamin Peterson51ce29c2013-04-15 21:38:25 -0400330 if ((newobj = newSHA1object()) == NULL)
331 return NULL;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000332
333 newobj->hash_state = self->hash_state;
334 return (PyObject *)newobj;
335}
336
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200337/*[clinic input]
338SHA1Type.digest
339
340Return the digest value as a string of binary data.
341[clinic start generated code]*/
342
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200343static PyObject *
344SHA1Type_digest_impl(SHA1object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300345/*[clinic end generated code: output=2f05302a7aa2b5cb input=205d47e1927fd009]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000346{
347 unsigned char digest[SHA1_DIGESTSIZE];
348 struct sha1_state temp;
349
350 temp = self->hash_state;
351 sha1_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000352 return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000353}
354
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200355/*[clinic input]
356SHA1Type.hexdigest
357
358Return the digest value as a string of hexadecimal digits.
359[clinic start generated code]*/
360
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200361static PyObject *
362SHA1Type_hexdigest_impl(SHA1object *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300363/*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000364{
365 unsigned char digest[SHA1_DIGESTSIZE];
366 struct sha1_state temp;
367 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200368 Py_UCS1 *hex_digest;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000369 int i, j;
370
371 /* Get the raw (binary) digest value */
372 temp = self->hash_state;
373 sha1_done(&temp, digest);
374
375 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200376 retval = PyUnicode_New(SHA1_DIGESTSIZE * 2, 127);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000377 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200379 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000380
381 /* Make hex version of the digest */
382 for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200383 unsigned char c;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000384 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200385 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000386 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200387 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000388 }
Christian Heimesf402e922013-01-03 09:21:55 +0100389#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200390 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100391#endif
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000392 return retval;
393}
394
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200395/*[clinic input]
396SHA1Type.update
397
398 obj: object
399 /
400
401Update this hash object's state with the provided string.
402[clinic start generated code]*/
403
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000404static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200405SHA1Type_update(SHA1object *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300406/*[clinic end generated code: output=d9902f0e5015e9ae input=aad8e07812edbba3]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000407{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000408 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000409
Gregory P. Smith365a1862009-02-12 07:35:29 +0000410 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
411
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000412 sha1_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000413
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000414 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000415 Py_INCREF(Py_None);
416 return Py_None;
417}
418
419static PyMethodDef SHA1_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200420 SHA1TYPE_COPY_METHODDEF
421 SHA1TYPE_DIGEST_METHODDEF
422 SHA1TYPE_HEXDIGEST_METHODDEF
423 SHA1TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000425};
426
427static PyObject *
428SHA1_get_block_size(PyObject *self, void *closure)
429{
Christian Heimes217cfd12007-12-02 14:31:20 +0000430 return PyLong_FromLong(SHA1_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000431}
432
433static PyObject *
434SHA1_get_name(PyObject *self, void *closure)
435{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200436 return PyUnicode_FromStringAndSize("sha1", 4);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000437}
438
439static PyObject *
440sha1_get_digest_size(PyObject *self, void *closure)
441{
Christian Heimes217cfd12007-12-02 14:31:20 +0000442 return PyLong_FromLong(SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000443}
444
445
446static PyGetSetDef SHA1_getseters[] = {
447 {"block_size",
448 (getter)SHA1_get_block_size, NULL,
449 NULL,
450 NULL},
451 {"name",
452 (getter)SHA1_get_name, NULL,
453 NULL,
454 NULL},
455 {"digest_size",
456 (getter)sha1_get_digest_size, NULL,
457 NULL,
458 NULL},
459 {NULL} /* Sentinel */
460};
461
462static PyTypeObject SHA1type = {
463 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 "_sha1.sha1", /*tp_name*/
465 sizeof(SHA1object), /*tp_size*/
466 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000467 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 SHA1_dealloc, /*tp_dealloc*/
469 0, /*tp_print*/
470 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000471 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000472 0, /*tp_reserved*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000473 0, /*tp_repr*/
474 0, /*tp_as_number*/
475 0, /*tp_as_sequence*/
476 0, /*tp_as_mapping*/
477 0, /*tp_hash*/
478 0, /*tp_call*/
479 0, /*tp_str*/
480 0, /*tp_getattro*/
481 0, /*tp_setattro*/
482 0, /*tp_as_buffer*/
483 Py_TPFLAGS_DEFAULT, /*tp_flags*/
484 0, /*tp_doc*/
485 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 0, /*tp_clear*/
487 0, /*tp_richcompare*/
488 0, /*tp_weaklistoffset*/
489 0, /*tp_iter*/
490 0, /*tp_iternext*/
491 SHA1_methods, /* tp_methods */
492 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000493 SHA1_getseters, /* tp_getset */
494};
495
496
497/* The single module-level function: new() */
498
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200499/*[clinic input]
500_sha1.sha1
501
502 string: object(c_default="NULL") = b''
503
504Return a new SHA1 hash object; optionally initialized with a string.
505[clinic start generated code]*/
506
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200507static PyObject *
508_sha1_sha1_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300509/*[clinic end generated code: output=3e4e841386b9e8db input=27ea54281d995ec2]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200510{
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000511 SHA1object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000512 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000513
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200514 if (string)
515 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000516
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000517 if ((new = newSHA1object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200518 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000519 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000520 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000521 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000522
523 sha1_init(&new->hash_state);
524
525 if (PyErr_Occurred()) {
526 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200527 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000528 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000529 return NULL;
530 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200531 if (string) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000532 sha1_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000533 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000534 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000535
536 return (PyObject *)new;
537}
538
539
540/* List of functions exported by this module */
541
542static struct PyMethodDef SHA1_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200543 _SHA1_SHA1_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000545};
546
547
548/* Initialize this module. */
549
550#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
551
Martin v. Löwis1a214512008-06-11 05:26:20 +0000552
553static struct PyModuleDef _sha1module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyModuleDef_HEAD_INIT,
555 "_sha1",
556 NULL,
557 -1,
558 SHA1_functions,
559 NULL,
560 NULL,
561 NULL,
562 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000563};
564
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000565PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000566PyInit__sha1(void)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000567{
Christian Heimes327dd732013-10-22 15:05:23 +0200568 PyObject *m;
569
Christian Heimes90aa7642007-12-19 02:45:37 +0000570 Py_TYPE(&SHA1type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000571 if (PyType_Ready(&SHA1type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200573
574 m = PyModule_Create(&_sha1module);
575 if (m == NULL)
576 return NULL;
577
578 Py_INCREF((PyObject *)&SHA1type);
579 PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);
580 return m;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000581}