1 | n/a | /* SHA1 module */ |
---|
2 | n/a | |
---|
3 | n/a | /* This module provides an interface to the SHA1 algorithm */ |
---|
4 | n/a | |
---|
5 | n/a | /* See below for information about the original code this module was |
---|
6 | n/a | based upon. Additional work performed by: |
---|
7 | n/a | |
---|
8 | n/a | Andrew Kuchling (amk@amk.ca) |
---|
9 | n/a | Greg Stein (gstein@lyra.org) |
---|
10 | n/a | Trevor Perrin (trevp@trevp.net) |
---|
11 | n/a | |
---|
12 | n/a | Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) |
---|
13 | n/a | Licensed to PSF under a Contributor Agreement. |
---|
14 | n/a | |
---|
15 | n/a | */ |
---|
16 | n/a | |
---|
17 | n/a | /* SHA1 objects */ |
---|
18 | n/a | |
---|
19 | n/a | #include "Python.h" |
---|
20 | n/a | #include "hashlib.h" |
---|
21 | n/a | #include "pystrhex.h" |
---|
22 | n/a | |
---|
23 | n/a | /*[clinic input] |
---|
24 | n/a | module _sha1 |
---|
25 | n/a | class SHA1Type "SHA1object *" "&PyType_Type" |
---|
26 | n/a | [clinic start generated code]*/ |
---|
27 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/ |
---|
28 | n/a | |
---|
29 | n/a | /* Some useful types */ |
---|
30 | n/a | |
---|
31 | n/a | #if SIZEOF_INT == 4 |
---|
32 | n/a | typedef unsigned int SHA1_INT32; /* 32-bit integer */ |
---|
33 | n/a | typedef long long SHA1_INT64; /* 64-bit integer */ |
---|
34 | n/a | #else |
---|
35 | n/a | /* not defined. compilation will die. */ |
---|
36 | n/a | #endif |
---|
37 | n/a | |
---|
38 | n/a | /* The SHA1 block size and message digest sizes, in bytes */ |
---|
39 | n/a | |
---|
40 | n/a | #define SHA1_BLOCKSIZE 64 |
---|
41 | n/a | #define SHA1_DIGESTSIZE 20 |
---|
42 | n/a | |
---|
43 | n/a | /* The structure for storing SHA1 info */ |
---|
44 | n/a | |
---|
45 | n/a | struct sha1_state { |
---|
46 | n/a | SHA1_INT64 length; |
---|
47 | n/a | SHA1_INT32 state[5], curlen; |
---|
48 | n/a | unsigned char buf[SHA1_BLOCKSIZE]; |
---|
49 | n/a | }; |
---|
50 | n/a | |
---|
51 | n/a | typedef struct { |
---|
52 | n/a | PyObject_HEAD |
---|
53 | n/a | |
---|
54 | n/a | struct sha1_state hash_state; |
---|
55 | n/a | } SHA1object; |
---|
56 | n/a | |
---|
57 | n/a | #include "clinic/sha1module.c.h" |
---|
58 | n/a | |
---|
59 | n/a | /* ------------------------------------------------------------------------ |
---|
60 | n/a | * |
---|
61 | n/a | * This code for the SHA1 algorithm was noted as public domain. The |
---|
62 | n/a | * original headers are pasted below. |
---|
63 | n/a | * |
---|
64 | n/a | * Several changes have been made to make it more compatible with the |
---|
65 | n/a | * Python environment and desired interface. |
---|
66 | n/a | * |
---|
67 | n/a | */ |
---|
68 | n/a | |
---|
69 | n/a | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
---|
70 | n/a | * |
---|
71 | n/a | * LibTomCrypt is a library that provides various cryptographic |
---|
72 | n/a | * algorithms in a highly modular and flexible manner. |
---|
73 | n/a | * |
---|
74 | n/a | * The library is free for all purposes without any express |
---|
75 | n/a | * guarantee it works. |
---|
76 | n/a | * |
---|
77 | n/a | * Tom St Denis, tomstdenis@gmail.com, http://libtom.org |
---|
78 | n/a | */ |
---|
79 | n/a | |
---|
80 | n/a | /* rotate the hard way (platform optimizations could be done) */ |
---|
81 | n/a | #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
---|
82 | n/a | #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
---|
83 | n/a | |
---|
84 | n/a | /* Endian Neutral macros that work on all platforms */ |
---|
85 | n/a | |
---|
86 | n/a | #define STORE32H(x, y) \ |
---|
87 | n/a | { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ |
---|
88 | n/a | (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } |
---|
89 | n/a | |
---|
90 | n/a | #define LOAD32H(x, y) \ |
---|
91 | n/a | { x = ((unsigned long)((y)[0] & 255)<<24) | \ |
---|
92 | n/a | ((unsigned long)((y)[1] & 255)<<16) | \ |
---|
93 | n/a | ((unsigned long)((y)[2] & 255)<<8) | \ |
---|
94 | n/a | ((unsigned long)((y)[3] & 255)); } |
---|
95 | n/a | |
---|
96 | n/a | #define STORE64H(x, y) \ |
---|
97 | n/a | { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
---|
98 | n/a | (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ |
---|
99 | n/a | (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ |
---|
100 | n/a | (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } |
---|
101 | n/a | |
---|
102 | n/a | |
---|
103 | n/a | /* SHA1 macros */ |
---|
104 | n/a | |
---|
105 | n/a | #define F0(x,y,z) (z ^ (x & (y ^ z))) |
---|
106 | n/a | #define F1(x,y,z) (x ^ y ^ z) |
---|
107 | n/a | #define F2(x,y,z) ((x & y) | (z & (x | y))) |
---|
108 | n/a | #define F3(x,y,z) (x ^ y ^ z) |
---|
109 | n/a | |
---|
110 | n/a | static void sha1_compress(struct sha1_state *sha1, unsigned char *buf) |
---|
111 | n/a | { |
---|
112 | n/a | SHA1_INT32 a,b,c,d,e,W[80],i; |
---|
113 | n/a | |
---|
114 | n/a | /* copy the state into 512-bits into W[0..15] */ |
---|
115 | n/a | for (i = 0; i < 16; i++) { |
---|
116 | n/a | LOAD32H(W[i], buf + (4*i)); |
---|
117 | n/a | } |
---|
118 | n/a | |
---|
119 | n/a | /* copy state */ |
---|
120 | n/a | a = sha1->state[0]; |
---|
121 | n/a | b = sha1->state[1]; |
---|
122 | n/a | c = sha1->state[2]; |
---|
123 | n/a | d = sha1->state[3]; |
---|
124 | n/a | e = sha1->state[4]; |
---|
125 | n/a | |
---|
126 | n/a | /* expand it */ |
---|
127 | n/a | for (i = 16; i < 80; i++) { |
---|
128 | n/a | W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); |
---|
129 | n/a | } |
---|
130 | n/a | |
---|
131 | n/a | /* compress */ |
---|
132 | n/a | /* round one */ |
---|
133 | n/a | #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); |
---|
134 | n/a | #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); |
---|
135 | n/a | #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); |
---|
136 | n/a | #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); |
---|
137 | n/a | |
---|
138 | n/a | for (i = 0; i < 20; ) { |
---|
139 | n/a | FF_0(a,b,c,d,e,i++); |
---|
140 | n/a | FF_0(e,a,b,c,d,i++); |
---|
141 | n/a | FF_0(d,e,a,b,c,i++); |
---|
142 | n/a | FF_0(c,d,e,a,b,i++); |
---|
143 | n/a | FF_0(b,c,d,e,a,i++); |
---|
144 | n/a | } |
---|
145 | n/a | |
---|
146 | n/a | /* round two */ |
---|
147 | n/a | for (; i < 40; ) { |
---|
148 | n/a | FF_1(a,b,c,d,e,i++); |
---|
149 | n/a | FF_1(e,a,b,c,d,i++); |
---|
150 | n/a | FF_1(d,e,a,b,c,i++); |
---|
151 | n/a | FF_1(c,d,e,a,b,i++); |
---|
152 | n/a | FF_1(b,c,d,e,a,i++); |
---|
153 | n/a | } |
---|
154 | n/a | |
---|
155 | n/a | /* round three */ |
---|
156 | n/a | for (; i < 60; ) { |
---|
157 | n/a | FF_2(a,b,c,d,e,i++); |
---|
158 | n/a | FF_2(e,a,b,c,d,i++); |
---|
159 | n/a | FF_2(d,e,a,b,c,i++); |
---|
160 | n/a | FF_2(c,d,e,a,b,i++); |
---|
161 | n/a | FF_2(b,c,d,e,a,i++); |
---|
162 | n/a | } |
---|
163 | n/a | |
---|
164 | n/a | /* round four */ |
---|
165 | n/a | for (; i < 80; ) { |
---|
166 | n/a | FF_3(a,b,c,d,e,i++); |
---|
167 | n/a | FF_3(e,a,b,c,d,i++); |
---|
168 | n/a | FF_3(d,e,a,b,c,i++); |
---|
169 | n/a | FF_3(c,d,e,a,b,i++); |
---|
170 | n/a | FF_3(b,c,d,e,a,i++); |
---|
171 | n/a | } |
---|
172 | n/a | |
---|
173 | n/a | #undef FF_0 |
---|
174 | n/a | #undef FF_1 |
---|
175 | n/a | #undef FF_2 |
---|
176 | n/a | #undef FF_3 |
---|
177 | n/a | |
---|
178 | n/a | /* store */ |
---|
179 | n/a | sha1->state[0] = sha1->state[0] + a; |
---|
180 | n/a | sha1->state[1] = sha1->state[1] + b; |
---|
181 | n/a | sha1->state[2] = sha1->state[2] + c; |
---|
182 | n/a | sha1->state[3] = sha1->state[3] + d; |
---|
183 | n/a | sha1->state[4] = sha1->state[4] + e; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | /** |
---|
187 | n/a | Initialize the hash state |
---|
188 | n/a | @param sha1 The hash state you wish to initialize |
---|
189 | n/a | */ |
---|
190 | n/a | static void |
---|
191 | n/a | sha1_init(struct sha1_state *sha1) |
---|
192 | n/a | { |
---|
193 | n/a | assert(sha1 != NULL); |
---|
194 | n/a | sha1->state[0] = 0x67452301UL; |
---|
195 | n/a | sha1->state[1] = 0xefcdab89UL; |
---|
196 | n/a | sha1->state[2] = 0x98badcfeUL; |
---|
197 | n/a | sha1->state[3] = 0x10325476UL; |
---|
198 | n/a | sha1->state[4] = 0xc3d2e1f0UL; |
---|
199 | n/a | sha1->curlen = 0; |
---|
200 | n/a | sha1->length = 0; |
---|
201 | n/a | } |
---|
202 | n/a | |
---|
203 | n/a | /** |
---|
204 | n/a | Process a block of memory though the hash |
---|
205 | n/a | @param sha1 The hash state |
---|
206 | n/a | @param in The data to hash |
---|
207 | n/a | @param inlen The length of the data (octets) |
---|
208 | n/a | */ |
---|
209 | n/a | static void |
---|
210 | n/a | sha1_process(struct sha1_state *sha1, |
---|
211 | n/a | const unsigned char *in, Py_ssize_t inlen) |
---|
212 | n/a | { |
---|
213 | n/a | Py_ssize_t n; |
---|
214 | n/a | |
---|
215 | n/a | assert(sha1 != NULL); |
---|
216 | n/a | assert(in != NULL); |
---|
217 | n/a | assert(sha1->curlen <= sizeof(sha1->buf)); |
---|
218 | n/a | |
---|
219 | n/a | while (inlen > 0) { |
---|
220 | n/a | if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) { |
---|
221 | n/a | sha1_compress(sha1, (unsigned char *)in); |
---|
222 | n/a | sha1->length += SHA1_BLOCKSIZE * 8; |
---|
223 | n/a | in += SHA1_BLOCKSIZE; |
---|
224 | n/a | inlen -= SHA1_BLOCKSIZE; |
---|
225 | n/a | } else { |
---|
226 | n/a | n = Py_MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen)); |
---|
227 | n/a | memcpy(sha1->buf + sha1->curlen, in, (size_t)n); |
---|
228 | n/a | sha1->curlen += (SHA1_INT32)n; |
---|
229 | n/a | in += n; |
---|
230 | n/a | inlen -= n; |
---|
231 | n/a | if (sha1->curlen == SHA1_BLOCKSIZE) { |
---|
232 | n/a | sha1_compress(sha1, sha1->buf); |
---|
233 | n/a | sha1->length += 8*SHA1_BLOCKSIZE; |
---|
234 | n/a | sha1->curlen = 0; |
---|
235 | n/a | } |
---|
236 | n/a | } |
---|
237 | n/a | } |
---|
238 | n/a | } |
---|
239 | n/a | |
---|
240 | n/a | /** |
---|
241 | n/a | Terminate the hash to get the digest |
---|
242 | n/a | @param sha1 The hash state |
---|
243 | n/a | @param out [out] The destination of the hash (20 bytes) |
---|
244 | n/a | */ |
---|
245 | n/a | static void |
---|
246 | n/a | sha1_done(struct sha1_state *sha1, unsigned char *out) |
---|
247 | n/a | { |
---|
248 | n/a | int i; |
---|
249 | n/a | |
---|
250 | n/a | assert(sha1 != NULL); |
---|
251 | n/a | assert(out != NULL); |
---|
252 | n/a | assert(sha1->curlen < sizeof(sha1->buf)); |
---|
253 | n/a | |
---|
254 | n/a | /* increase the length of the message */ |
---|
255 | n/a | sha1->length += sha1->curlen * 8; |
---|
256 | n/a | |
---|
257 | n/a | /* append the '1' bit */ |
---|
258 | n/a | sha1->buf[sha1->curlen++] = (unsigned char)0x80; |
---|
259 | n/a | |
---|
260 | n/a | /* if the length is currently above 56 bytes we append zeros |
---|
261 | n/a | * then compress. Then we can fall back to padding zeros and length |
---|
262 | n/a | * encoding like normal. |
---|
263 | n/a | */ |
---|
264 | n/a | if (sha1->curlen > 56) { |
---|
265 | n/a | while (sha1->curlen < 64) { |
---|
266 | n/a | sha1->buf[sha1->curlen++] = (unsigned char)0; |
---|
267 | n/a | } |
---|
268 | n/a | sha1_compress(sha1, sha1->buf); |
---|
269 | n/a | sha1->curlen = 0; |
---|
270 | n/a | } |
---|
271 | n/a | |
---|
272 | n/a | /* pad upto 56 bytes of zeroes */ |
---|
273 | n/a | while (sha1->curlen < 56) { |
---|
274 | n/a | sha1->buf[sha1->curlen++] = (unsigned char)0; |
---|
275 | n/a | } |
---|
276 | n/a | |
---|
277 | n/a | /* store length */ |
---|
278 | n/a | STORE64H(sha1->length, sha1->buf+56); |
---|
279 | n/a | sha1_compress(sha1, sha1->buf); |
---|
280 | n/a | |
---|
281 | n/a | /* copy output */ |
---|
282 | n/a | for (i = 0; i < 5; i++) { |
---|
283 | n/a | STORE32H(sha1->state[i], out+(4*i)); |
---|
284 | n/a | } |
---|
285 | n/a | } |
---|
286 | n/a | |
---|
287 | n/a | |
---|
288 | n/a | /* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */ |
---|
289 | n/a | /* .Revision: 1.10 $ */ |
---|
290 | n/a | /* .Date: 2007/05/12 14:25:28 $ */ |
---|
291 | n/a | |
---|
292 | n/a | /* |
---|
293 | n/a | * End of copied SHA1 code. |
---|
294 | n/a | * |
---|
295 | n/a | * ------------------------------------------------------------------------ |
---|
296 | n/a | */ |
---|
297 | n/a | |
---|
298 | n/a | static PyTypeObject SHA1type; |
---|
299 | n/a | |
---|
300 | n/a | |
---|
301 | n/a | static SHA1object * |
---|
302 | n/a | newSHA1object(void) |
---|
303 | n/a | { |
---|
304 | n/a | return (SHA1object *)PyObject_New(SHA1object, &SHA1type); |
---|
305 | n/a | } |
---|
306 | n/a | |
---|
307 | n/a | |
---|
308 | n/a | /* Internal methods for a hash object */ |
---|
309 | n/a | |
---|
310 | n/a | static void |
---|
311 | n/a | SHA1_dealloc(PyObject *ptr) |
---|
312 | n/a | { |
---|
313 | n/a | PyObject_Del(ptr); |
---|
314 | n/a | } |
---|
315 | n/a | |
---|
316 | n/a | |
---|
317 | n/a | /* External methods for a hash object */ |
---|
318 | n/a | |
---|
319 | n/a | /*[clinic input] |
---|
320 | n/a | SHA1Type.copy |
---|
321 | n/a | |
---|
322 | n/a | Return a copy of the hash object. |
---|
323 | n/a | [clinic start generated code]*/ |
---|
324 | n/a | |
---|
325 | n/a | static PyObject * |
---|
326 | n/a | SHA1Type_copy_impl(SHA1object *self) |
---|
327 | n/a | /*[clinic end generated code: output=b4e001264620f02a input=b7eae10df6f89b36]*/ |
---|
328 | n/a | { |
---|
329 | n/a | SHA1object *newobj; |
---|
330 | n/a | |
---|
331 | n/a | if ((newobj = newSHA1object()) == NULL) |
---|
332 | n/a | return NULL; |
---|
333 | n/a | |
---|
334 | n/a | newobj->hash_state = self->hash_state; |
---|
335 | n/a | return (PyObject *)newobj; |
---|
336 | n/a | } |
---|
337 | n/a | |
---|
338 | n/a | /*[clinic input] |
---|
339 | n/a | SHA1Type.digest |
---|
340 | n/a | |
---|
341 | n/a | Return the digest value as a string of binary data. |
---|
342 | n/a | [clinic start generated code]*/ |
---|
343 | n/a | |
---|
344 | n/a | static PyObject * |
---|
345 | n/a | SHA1Type_digest_impl(SHA1object *self) |
---|
346 | n/a | /*[clinic end generated code: output=2f05302a7aa2b5cb input=205d47e1927fd009]*/ |
---|
347 | n/a | { |
---|
348 | n/a | unsigned char digest[SHA1_DIGESTSIZE]; |
---|
349 | n/a | struct sha1_state temp; |
---|
350 | n/a | |
---|
351 | n/a | temp = self->hash_state; |
---|
352 | n/a | sha1_done(&temp, digest); |
---|
353 | n/a | return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); |
---|
354 | n/a | } |
---|
355 | n/a | |
---|
356 | n/a | /*[clinic input] |
---|
357 | n/a | SHA1Type.hexdigest |
---|
358 | n/a | |
---|
359 | n/a | Return the digest value as a string of hexadecimal digits. |
---|
360 | n/a | [clinic start generated code]*/ |
---|
361 | n/a | |
---|
362 | n/a | static PyObject * |
---|
363 | n/a | SHA1Type_hexdigest_impl(SHA1object *self) |
---|
364 | n/a | /*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/ |
---|
365 | n/a | { |
---|
366 | n/a | unsigned char digest[SHA1_DIGESTSIZE]; |
---|
367 | n/a | struct sha1_state temp; |
---|
368 | n/a | |
---|
369 | n/a | /* Get the raw (binary) digest value */ |
---|
370 | n/a | temp = self->hash_state; |
---|
371 | n/a | sha1_done(&temp, digest); |
---|
372 | n/a | |
---|
373 | n/a | return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE); |
---|
374 | n/a | } |
---|
375 | n/a | |
---|
376 | n/a | /*[clinic input] |
---|
377 | n/a | SHA1Type.update |
---|
378 | n/a | |
---|
379 | n/a | obj: object |
---|
380 | n/a | / |
---|
381 | n/a | |
---|
382 | n/a | Update this hash object's state with the provided string. |
---|
383 | n/a | [clinic start generated code]*/ |
---|
384 | n/a | |
---|
385 | n/a | static PyObject * |
---|
386 | n/a | SHA1Type_update(SHA1object *self, PyObject *obj) |
---|
387 | n/a | /*[clinic end generated code: output=d9902f0e5015e9ae input=aad8e07812edbba3]*/ |
---|
388 | n/a | { |
---|
389 | n/a | Py_buffer buf; |
---|
390 | n/a | |
---|
391 | n/a | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
---|
392 | n/a | |
---|
393 | n/a | sha1_process(&self->hash_state, buf.buf, buf.len); |
---|
394 | n/a | |
---|
395 | n/a | PyBuffer_Release(&buf); |
---|
396 | n/a | Py_RETURN_NONE; |
---|
397 | n/a | } |
---|
398 | n/a | |
---|
399 | n/a | static PyMethodDef SHA1_methods[] = { |
---|
400 | n/a | SHA1TYPE_COPY_METHODDEF |
---|
401 | n/a | SHA1TYPE_DIGEST_METHODDEF |
---|
402 | n/a | SHA1TYPE_HEXDIGEST_METHODDEF |
---|
403 | n/a | SHA1TYPE_UPDATE_METHODDEF |
---|
404 | n/a | {NULL, NULL} /* sentinel */ |
---|
405 | n/a | }; |
---|
406 | n/a | |
---|
407 | n/a | static PyObject * |
---|
408 | n/a | SHA1_get_block_size(PyObject *self, void *closure) |
---|
409 | n/a | { |
---|
410 | n/a | return PyLong_FromLong(SHA1_BLOCKSIZE); |
---|
411 | n/a | } |
---|
412 | n/a | |
---|
413 | n/a | static PyObject * |
---|
414 | n/a | SHA1_get_name(PyObject *self, void *closure) |
---|
415 | n/a | { |
---|
416 | n/a | return PyUnicode_FromStringAndSize("sha1", 4); |
---|
417 | n/a | } |
---|
418 | n/a | |
---|
419 | n/a | static PyObject * |
---|
420 | n/a | sha1_get_digest_size(PyObject *self, void *closure) |
---|
421 | n/a | { |
---|
422 | n/a | return PyLong_FromLong(SHA1_DIGESTSIZE); |
---|
423 | n/a | } |
---|
424 | n/a | |
---|
425 | n/a | |
---|
426 | n/a | static PyGetSetDef SHA1_getseters[] = { |
---|
427 | n/a | {"block_size", |
---|
428 | n/a | (getter)SHA1_get_block_size, NULL, |
---|
429 | n/a | NULL, |
---|
430 | n/a | NULL}, |
---|
431 | n/a | {"name", |
---|
432 | n/a | (getter)SHA1_get_name, NULL, |
---|
433 | n/a | NULL, |
---|
434 | n/a | NULL}, |
---|
435 | n/a | {"digest_size", |
---|
436 | n/a | (getter)sha1_get_digest_size, NULL, |
---|
437 | n/a | NULL, |
---|
438 | n/a | NULL}, |
---|
439 | n/a | {NULL} /* Sentinel */ |
---|
440 | n/a | }; |
---|
441 | n/a | |
---|
442 | n/a | static PyTypeObject SHA1type = { |
---|
443 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
444 | n/a | "_sha1.sha1", /*tp_name*/ |
---|
445 | n/a | sizeof(SHA1object), /*tp_size*/ |
---|
446 | n/a | 0, /*tp_itemsize*/ |
---|
447 | n/a | /* methods */ |
---|
448 | n/a | SHA1_dealloc, /*tp_dealloc*/ |
---|
449 | n/a | 0, /*tp_print*/ |
---|
450 | n/a | 0, /*tp_getattr*/ |
---|
451 | n/a | 0, /*tp_setattr*/ |
---|
452 | n/a | 0, /*tp_reserved*/ |
---|
453 | n/a | 0, /*tp_repr*/ |
---|
454 | n/a | 0, /*tp_as_number*/ |
---|
455 | n/a | 0, /*tp_as_sequence*/ |
---|
456 | n/a | 0, /*tp_as_mapping*/ |
---|
457 | n/a | 0, /*tp_hash*/ |
---|
458 | n/a | 0, /*tp_call*/ |
---|
459 | n/a | 0, /*tp_str*/ |
---|
460 | n/a | 0, /*tp_getattro*/ |
---|
461 | n/a | 0, /*tp_setattro*/ |
---|
462 | n/a | 0, /*tp_as_buffer*/ |
---|
463 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
464 | n/a | 0, /*tp_doc*/ |
---|
465 | n/a | 0, /*tp_traverse*/ |
---|
466 | n/a | 0, /*tp_clear*/ |
---|
467 | n/a | 0, /*tp_richcompare*/ |
---|
468 | n/a | 0, /*tp_weaklistoffset*/ |
---|
469 | n/a | 0, /*tp_iter*/ |
---|
470 | n/a | 0, /*tp_iternext*/ |
---|
471 | n/a | SHA1_methods, /* tp_methods */ |
---|
472 | n/a | NULL, /* tp_members */ |
---|
473 | n/a | SHA1_getseters, /* tp_getset */ |
---|
474 | n/a | }; |
---|
475 | n/a | |
---|
476 | n/a | |
---|
477 | n/a | /* The single module-level function: new() */ |
---|
478 | n/a | |
---|
479 | n/a | /*[clinic input] |
---|
480 | n/a | _sha1.sha1 |
---|
481 | n/a | |
---|
482 | n/a | string: object(c_default="NULL") = b'' |
---|
483 | n/a | |
---|
484 | n/a | Return a new SHA1 hash object; optionally initialized with a string. |
---|
485 | n/a | [clinic start generated code]*/ |
---|
486 | n/a | |
---|
487 | n/a | static PyObject * |
---|
488 | n/a | _sha1_sha1_impl(PyObject *module, PyObject *string) |
---|
489 | n/a | /*[clinic end generated code: output=e5982830d1dece51 input=27ea54281d995ec2]*/ |
---|
490 | n/a | { |
---|
491 | n/a | SHA1object *new; |
---|
492 | n/a | Py_buffer buf; |
---|
493 | n/a | |
---|
494 | n/a | if (string) |
---|
495 | n/a | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
---|
496 | n/a | |
---|
497 | n/a | if ((new = newSHA1object()) == NULL) { |
---|
498 | n/a | if (string) |
---|
499 | n/a | PyBuffer_Release(&buf); |
---|
500 | n/a | return NULL; |
---|
501 | n/a | } |
---|
502 | n/a | |
---|
503 | n/a | sha1_init(&new->hash_state); |
---|
504 | n/a | |
---|
505 | n/a | if (PyErr_Occurred()) { |
---|
506 | n/a | Py_DECREF(new); |
---|
507 | n/a | if (string) |
---|
508 | n/a | PyBuffer_Release(&buf); |
---|
509 | n/a | return NULL; |
---|
510 | n/a | } |
---|
511 | n/a | if (string) { |
---|
512 | n/a | sha1_process(&new->hash_state, buf.buf, buf.len); |
---|
513 | n/a | PyBuffer_Release(&buf); |
---|
514 | n/a | } |
---|
515 | n/a | |
---|
516 | n/a | return (PyObject *)new; |
---|
517 | n/a | } |
---|
518 | n/a | |
---|
519 | n/a | |
---|
520 | n/a | /* List of functions exported by this module */ |
---|
521 | n/a | |
---|
522 | n/a | static struct PyMethodDef SHA1_functions[] = { |
---|
523 | n/a | _SHA1_SHA1_METHODDEF |
---|
524 | n/a | {NULL, NULL} /* Sentinel */ |
---|
525 | n/a | }; |
---|
526 | n/a | |
---|
527 | n/a | |
---|
528 | n/a | /* Initialize this module. */ |
---|
529 | n/a | |
---|
530 | n/a | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
---|
531 | n/a | |
---|
532 | n/a | |
---|
533 | n/a | static struct PyModuleDef _sha1module = { |
---|
534 | n/a | PyModuleDef_HEAD_INIT, |
---|
535 | n/a | "_sha1", |
---|
536 | n/a | NULL, |
---|
537 | n/a | -1, |
---|
538 | n/a | SHA1_functions, |
---|
539 | n/a | NULL, |
---|
540 | n/a | NULL, |
---|
541 | n/a | NULL, |
---|
542 | n/a | NULL |
---|
543 | n/a | }; |
---|
544 | n/a | |
---|
545 | n/a | PyMODINIT_FUNC |
---|
546 | n/a | PyInit__sha1(void) |
---|
547 | n/a | { |
---|
548 | n/a | PyObject *m; |
---|
549 | n/a | |
---|
550 | n/a | Py_TYPE(&SHA1type) = &PyType_Type; |
---|
551 | n/a | if (PyType_Ready(&SHA1type) < 0) |
---|
552 | n/a | return NULL; |
---|
553 | n/a | |
---|
554 | n/a | m = PyModule_Create(&_sha1module); |
---|
555 | n/a | if (m == NULL) |
---|
556 | n/a | return NULL; |
---|
557 | n/a | |
---|
558 | n/a | Py_INCREF((PyObject *)&SHA1type); |
---|
559 | n/a | PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type); |
---|
560 | n/a | return m; |
---|
561 | n/a | } |
---|