blob: 6f7a60de8775e36d4ff4fde6039280a1d00dad11 [file] [log] [blame]
Guido van Rossum29d2acc1999-03-24 19:03:59 +00001/***********************************************************
2Copyright 1999 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossum29d2acc1999-03-24 19:03:59 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum29d2acc1999-03-24 19:03:59 +000014
15******************************************************************/
16
17/* SHA module */
18
19/* This module provides an interface to NIST's Secure Hash Algorithm */
20
21/* See below for information about the original code this module was
22 based upon. Additional work performed by:
23
Andrew M. Kuchlinga4e9a332000-08-01 01:26:02 +000024 Andrew Kuchling (amk1@bigfoot.com)
Guido van Rossum29d2acc1999-03-24 19:03:59 +000025 Greg Stein (gstein@lyra.org)
26*/
27
28/* SHA objects */
29
30#include "Python.h"
31
32
33/* Endianness testing and definitions */
34#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
35 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
36
37#define PCT_LITTLE_ENDIAN 1
38#define PCT_BIG_ENDIAN 0
39
40/* Some useful types */
41
42typedef unsigned char SHA_BYTE;
43
44#if SIZEOF_INT == 4
45typedef unsigned int SHA_INT32; /* 32-bit integer */
46#else
47/* not defined. compilation will die. */
48#endif
49
50/* The SHA block size and message digest sizes, in bytes */
51
52#define SHA_BLOCKSIZE 64
53#define SHA_DIGESTSIZE 20
54
55/* The structure for storing SHS info */
56
57typedef struct {
Fred Drake2c4a3dc2000-07-08 06:41:03 +000058 PyObject_HEAD
59 SHA_INT32 digest[5]; /* Message digest */
60 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
61 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
62 int Endianness;
63 int local; /* unprocessed amount in data */
Guido van Rossum29d2acc1999-03-24 19:03:59 +000064} SHAobject;
65
66/* When run on a little-endian CPU we need to perform byte reversal on an
67 array of longwords. */
68
Fred Drake2c4a3dc2000-07-08 06:41:03 +000069static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
Guido van Rossum29d2acc1999-03-24 19:03:59 +000070{
71 SHA_INT32 value;
72
73 if ( Endianness == PCT_BIG_ENDIAN )
74 return;
75
76 byteCount /= sizeof(*buffer);
Fred Drake2c4a3dc2000-07-08 06:41:03 +000077 while (byteCount--) {
Guido van Rossum29d2acc1999-03-24 19:03:59 +000078 value = *buffer;
79 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
80 ( ( value & 0x00FF00FFL ) << 8 );
81 *buffer++ = ( value << 16 ) | ( value >> 16 );
82 }
83}
84
Fred Drake2c4a3dc2000-07-08 06:41:03 +000085static void SHAcopy(SHAobject *src, SHAobject *dest)
Guido van Rossum29d2acc1999-03-24 19:03:59 +000086{
Fred Drake2c4a3dc2000-07-08 06:41:03 +000087 dest->Endianness = src->Endianness;
88 dest->local = src->local;
89 dest->count_lo = src->count_lo;
90 dest->count_hi = src->count_hi;
91 memcpy(dest->digest, src->digest, sizeof(src->digest));
92 memcpy(dest->data, src->data, sizeof(src->data));
Guido van Rossum29d2acc1999-03-24 19:03:59 +000093}
94
95
96/* ------------------------------------------------------------------------
97 *
98 * This code for the SHA algorithm was noted as public domain. The original
99 * headers are pasted below.
100 *
101 * Several changes have been made to make it more compatible with the
102 * Python environment and desired interface.
103 *
104 */
105
106/* NIST Secure Hash Algorithm */
107/* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
108/* from Peter C. Gutmann's implementation as found in */
109/* Applied Cryptography by Bruce Schneier */
110/* Further modifications to include the "UNRAVEL" stuff, below */
111
112/* This code is in the public domain */
113
114/* UNRAVEL should be fastest & biggest */
115/* UNROLL_LOOPS should be just as big, but slightly slower */
116/* both undefined should be smallest and slowest */
117
118#define UNRAVEL
119/* #define UNROLL_LOOPS */
120
121/* The SHA f()-functions. The f1 and f3 functions can be optimized to
122 save one boolean operation each - thanks to Rich Schroeppel,
123 rcs@cs.arizona.edu for discovering this */
124
125/*#define f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
126#define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */
127#define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */
128/*#define f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
129#define f3(x,y,z) ((x & y) | (z & (x | y))) /* Rounds 40-59 */
130#define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */
131
132/* SHA constants */
133
134#define CONST1 0x5a827999L /* Rounds 0-19 */
135#define CONST2 0x6ed9eba1L /* Rounds 20-39 */
136#define CONST3 0x8f1bbcdcL /* Rounds 40-59 */
137#define CONST4 0xca62c1d6L /* Rounds 60-79 */
138
139/* 32-bit rotate */
140
141#define R32(x,n) ((x << n) | (x >> (32 - n)))
142
143/* the generic case, for when the overall rotation is not unraveled */
144
145#define FG(n) \
146 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; \
147 E = D; D = C; C = R32(B,30); B = A; A = T
148
149/* specific cases, for when the overall rotation is unraveled */
150
151#define FA(n) \
152 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30)
153
154#define FB(n) \
155 E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30)
156
157#define FC(n) \
158 D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30)
159
160#define FD(n) \
161 C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30)
162
163#define FE(n) \
164 B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30)
165
166#define FT(n) \
167 A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30)
168
169/* do SHA transformation */
170
171static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000172sha_transform(SHAobject *sha_info)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000173{
174 int i;
175 SHA_INT32 T, A, B, C, D, E, W[80], *WP;
176
177 memcpy(W, sha_info->data, sizeof(sha_info->data));
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000178 longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000179
180 for (i = 16; i < 80; ++i) {
181 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
182
183 /* extra rotation fix */
184 W[i] = R32(W[i], 1);
185 }
186 A = sha_info->digest[0];
187 B = sha_info->digest[1];
188 C = sha_info->digest[2];
189 D = sha_info->digest[3];
190 E = sha_info->digest[4];
191 WP = W;
192#ifdef UNRAVEL
193 FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
194 FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
195 FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
196 FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
197 FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
198 FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
199 FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
200 FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
201 sha_info->digest[0] += E;
202 sha_info->digest[1] += T;
203 sha_info->digest[2] += A;
204 sha_info->digest[3] += B;
205 sha_info->digest[4] += C;
206#else /* !UNRAVEL */
207#ifdef UNROLL_LOOPS
208 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
209 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
210 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
211 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
212 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
213 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
214 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
215 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
216#else /* !UNROLL_LOOPS */
217 for (i = 0; i < 20; ++i) { FG(1); }
218 for (i = 20; i < 40; ++i) { FG(2); }
219 for (i = 40; i < 60; ++i) { FG(3); }
220 for (i = 60; i < 80; ++i) { FG(4); }
221#endif /* !UNROLL_LOOPS */
222 sha_info->digest[0] += A;
223 sha_info->digest[1] += B;
224 sha_info->digest[2] += C;
225 sha_info->digest[3] += D;
226 sha_info->digest[4] += E;
227#endif /* !UNRAVEL */
228}
229
230/* initialize the SHA digest */
231
232static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000233sha_init(SHAobject *sha_info)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000234{
235 TestEndianness(sha_info->Endianness)
236
237 sha_info->digest[0] = 0x67452301L;
238 sha_info->digest[1] = 0xefcdab89L;
239 sha_info->digest[2] = 0x98badcfeL;
240 sha_info->digest[3] = 0x10325476L;
241 sha_info->digest[4] = 0xc3d2e1f0L;
242 sha_info->count_lo = 0L;
243 sha_info->count_hi = 0L;
244 sha_info->local = 0;
245}
246
247/* update the SHA digest */
248
249static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000250sha_update(SHAobject *sha_info, SHA_BYTE *buffer, int count)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000251{
252 int i;
253 SHA_INT32 clo;
254
255 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
256 if (clo < sha_info->count_lo) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000257 ++sha_info->count_hi;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000258 }
259 sha_info->count_lo = clo;
260 sha_info->count_hi += (SHA_INT32) count >> 29;
261 if (sha_info->local) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000262 i = SHA_BLOCKSIZE - sha_info->local;
263 if (i > count) {
264 i = count;
265 }
266 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
267 count -= i;
268 buffer += i;
269 sha_info->local += i;
270 if (sha_info->local == SHA_BLOCKSIZE) {
271 sha_transform(sha_info);
272 }
273 else {
274 return;
275 }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000276 }
277 while (count >= SHA_BLOCKSIZE) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000278 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
279 buffer += SHA_BLOCKSIZE;
280 count -= SHA_BLOCKSIZE;
281 sha_transform(sha_info);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000282 }
283 memcpy(sha_info->data, buffer, count);
284 sha_info->local = count;
285}
286
287/* finish computing the SHA digest */
288
289static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000290sha_final(unsigned char digest[20], SHAobject *sha_info)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000291{
292 int count;
293 SHA_INT32 lo_bit_count, hi_bit_count;
294
295 lo_bit_count = sha_info->count_lo;
296 hi_bit_count = sha_info->count_hi;
297 count = (int) ((lo_bit_count >> 3) & 0x3f);
298 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000299 if (count > SHA_BLOCKSIZE - 8) {
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000300 memset(((SHA_BYTE *) sha_info->data) + count, 0,
301 SHA_BLOCKSIZE - count);
302 sha_transform(sha_info);
303 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
304 }
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000305 else {
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000306 memset(((SHA_BYTE *) sha_info->data) + count, 0,
307 SHA_BLOCKSIZE - 8 - count);
308 }
309
310 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
311 swap these values into host-order. */
312 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
313 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
314 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
315 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
316 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
317 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
318 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
319 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
320 sha_transform(sha_info);
321 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
322 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
323 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
324 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
325 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
326 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
327 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
328 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
329 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
330 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
331 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
332 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
333 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
334 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
335 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
336 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
337 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
338 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
339 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
340 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
341}
342
343/*
344 * End of copied SHA code.
345 *
346 * ------------------------------------------------------------------------
347 */
348
349staticforward PyTypeObject SHAtype;
350
351
352static SHAobject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000353newSHAobject(void)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000354{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000355 return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000356}
357
358/* Internal methods for a hashing object */
359
360static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000361SHA_dealloc(PyObject *ptr)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000362{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000363 PyObject_Del(ptr);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000364}
365
366
367/* External methods for a hashing object */
368
369static char SHA_copy__doc__[] =
370"Return a copy of the hashing object.";
371
372static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000373SHA_copy(SHAobject *self, PyObject *args)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000374{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000375 SHAobject *newobj;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000376
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000377 if (!PyArg_ParseTuple(args, ":copy")) {
378 return NULL;
379 }
380 if ( (newobj = newSHAobject())==NULL)
381 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000382
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000383 SHAcopy(self, newobj);
384 return (PyObject *)newobj;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000385}
386
387static char SHA_digest__doc__[] =
388"Return the digest value as a string of binary data.";
389
390static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000391SHA_digest(SHAobject *self, PyObject *args)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000392{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000393 unsigned char digest[SHA_DIGESTSIZE];
394 SHAobject temp;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000395
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000396 if (!PyArg_ParseTuple(args, ":digest"))
397 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000398
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000399 SHAcopy(self, &temp);
400 sha_final(digest, &temp);
401 return PyString_FromStringAndSize((const char *)digest, sizeof(digest));
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000402}
403
404static char SHA_hexdigest__doc__[] =
405"Return the digest value as a string of hexadecimal digits.";
406
407static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000408SHA_hexdigest(SHAobject *self, PyObject *args)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000409{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000410 unsigned char digest[SHA_DIGESTSIZE];
411 SHAobject temp;
412 PyObject *retval;
413 char *hex_digest;
414 int i, j;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000415
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000416 if (!PyArg_ParseTuple(args, ":hexdigest"))
417 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000418
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000419 /* Get the raw (binary) digest value */
420 SHAcopy(self, &temp);
421 sha_final(digest, &temp);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000422
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000423 /* Create a new string */
424 retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
425 hex_digest = PyString_AsString(retval);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000426
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000427 /* Make hex version of the digest */
428 for(i=j=0; i<sizeof(digest); i++) {
429 char c;
430 c = digest[i] / 16; c = (c>9) ? c+'a'-10 : c + '0';
431 hex_digest[j++] = c;
432 c = digest[i] % 16; c = (c>9) ? c+'a'-10 : c + '0';
433 hex_digest[j++] = c;
434 }
435 return retval;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000436}
437
438static char SHA_update__doc__[] =
439"Update this hashing object's state with the provided string.";
440
441static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000442SHA_update(SHAobject *self, PyObject *args)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000443{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000444 unsigned char *cp;
445 int len;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000446
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000447 if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
448 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000449
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000450 sha_update(self, cp, len);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000451
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000452 Py_INCREF(Py_None);
453 return Py_None;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000454}
455
456static PyMethodDef SHA_methods[] = {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000457 {"copy", (PyCFunction)SHA_copy, METH_VARARGS, SHA_copy__doc__},
458 {"digest", (PyCFunction)SHA_digest, METH_VARARGS, SHA_digest__doc__},
459 {"hexdigest", (PyCFunction)SHA_hexdigest, METH_VARARGS, SHA_hexdigest__doc__},
460 {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__},
461 {NULL, NULL} /* sentinel */
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000462};
463
464static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000465SHA_getattr(PyObject *self, char *name)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000466{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000467 if (strcmp(name, "blocksize")==0)
468 return PyInt_FromLong(1);
469 if (strcmp(name, "digestsize")==0)
470 return PyInt_FromLong(20);
471
472 return Py_FindMethod(SHA_methods, self, name);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000473}
474
475static PyTypeObject SHAtype = {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000476 PyObject_HEAD_INIT(NULL)
477 0, /*ob_size*/
478 "SHA", /*tp_name*/
479 sizeof(SHAobject), /*tp_size*/
480 0, /*tp_itemsize*/
481 /* methods */
482 SHA_dealloc, /*tp_dealloc*/
483 0, /*tp_print*/
484 SHA_getattr, /*tp_getattr*/
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000485};
486
487
488/* The single module-level function: new() */
489
490static char SHA_new__doc__[] =
491 "Return a new SHA hashing object. An optional string "
492 "argument may be provided; if present, this string will be "
493 " automatically hashed.";
494
495static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000496SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000497{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000498 static char *kwlist[] = {"string", NULL};
499 SHAobject *new;
500 unsigned char *cp = NULL;
501 int len;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000502
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000503 if ((new = newSHAobject()) == NULL)
504 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000505
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000506 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
507 &cp, &len)) {
508 Py_DECREF(new);
509 return NULL;
510 }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000511
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000512 sha_init(new);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000513
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000514 if (PyErr_Occurred()) {
515 Py_DECREF(new);
516 return NULL;
517 }
518 if (cp)
519 sha_update(new, cp, len);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000520
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000521 return (PyObject *)new;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000522}
523
524
525/* List of functions exported by this module */
526
527static struct PyMethodDef SHA_functions[] = {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000528 {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
529 {"sha", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
530 {NULL, NULL} /* Sentinel */
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000531};
532
533
534/* Initialize this module. */
535
536#define insint(n,v) { PyObject *o=PyInt_FromLong(v); \
537 if (o!=NULL) PyDict_SetItemString(d,n,o); \
538 Py_XDECREF(o); }
539
540void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000541initsha(void)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000542{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000543 PyObject *d, *m;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000544
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000545 SHAtype.ob_type = &PyType_Type;
546 m = Py_InitModule("sha", SHA_functions);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000547
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000548 /* Add some symbolic constants to the module */
549 d = PyModule_GetDict(m);
550 insint("blocksize", 1); /* For future use, in case some hash
551 functions require an integral number of
552 blocks */
553 insint("digestsize", 20);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000554
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000555 /* Check for errors */
556 if (PyErr_Occurred())
557 Py_FatalError("can't initialize module SHA");
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000558}