annotate Modules/shamodule.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 6c08bfe78fff
children ff5bc2fa38d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* SHA module */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* This module provides an interface to NIST's Secure Hash Algorithm */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 /* See below for information about the original code this module was
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 based upon. Additional work performed by:
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
25966
5b828ffe1b35 Use personal e-mail address
Andrew M. Kuchling <amk@amk.ca>
parents: 24471
diff changeset
8 Andrew Kuchling (amk@amk.ca)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 Greg Stein (gstein@lyra.org)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
10
43298
a11b4885b445 email address update
Gregory P. Smith <greg@mad-scientist.com>
parents: 42333
diff changeset
11 Copyright (C) 2005 Gregory P. Smith (greg@krypto.org)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
12 Licensed to PSF under a Contributor Agreement.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
13
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 /* SHA objects */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #include "Python.h"
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
19 #include "structmember.h"
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
20 #include "hashlib.h"
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 /* Endianness testing and definitions */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #define PCT_LITTLE_ENDIAN 1
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 #define PCT_BIG_ENDIAN 0
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 /* Some useful types */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 typedef unsigned char SHA_BYTE;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #if SIZEOF_INT == 4
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 typedef unsigned int SHA_INT32; /* 32-bit integer */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #else
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 /* not defined. compilation will die. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 #endif
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 /* The SHA block size and message digest sizes, in bytes */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 #define SHA_BLOCKSIZE 64
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 #define SHA_DIGESTSIZE 20
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 /* The structure for storing SHS info */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 typedef struct {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
48 PyObject_HEAD
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
49 SHA_INT32 digest[5]; /* Message digest */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
50 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
51 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
52 int Endianness;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
53 int local; /* unprocessed amount in data */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 } SHAobject;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 /* When run on a little-endian CPU we need to perform byte reversal on an
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 array of longwords. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
59 static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 SHA_INT32 value;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if ( Endianness == PCT_BIG_ENDIAN )
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 byteCount /= sizeof(*buffer);
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
67 while (byteCount--) {
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 value = *buffer;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 ( ( value & 0x00FF00FFL ) << 8 );
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 *buffer++ = ( value << 16 ) | ( value >> 16 );
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
75 static void SHAcopy(SHAobject *src, SHAobject *dest)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
77 dest->Endianness = src->Endianness;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
78 dest->local = src->local;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
79 dest->count_lo = src->count_lo;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
80 dest->count_hi = src->count_hi;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
81 memcpy(dest->digest, src->digest, sizeof(src->digest));
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
82 memcpy(dest->data, src->data, sizeof(src->data));
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 /* ------------------------------------------------------------------------
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 *
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 * This code for the SHA algorithm was noted as public domain. The original
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 * headers are pasted below.
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 *
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 * Several changes have been made to make it more compatible with the
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 * Python environment and desired interface.
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 *
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 /* NIST Secure Hash Algorithm */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 /* from Peter C. Gutmann's implementation as found in */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 /* Applied Cryptography by Bruce Schneier */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 /* Further modifications to include the "UNRAVEL" stuff, below */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 /* This code is in the public domain */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 /* UNRAVEL should be fastest & biggest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 /* UNROLL_LOOPS should be just as big, but slightly slower */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 /* both undefined should be smallest and slowest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 #define UNRAVEL
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 /* #define UNROLL_LOOPS */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 /* The SHA f()-functions. The f1 and f3 functions can be optimized to
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 save one boolean operation each - thanks to Rich Schroeppel,
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 rcs@cs.arizona.edu for discovering this */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 /*#define f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 #define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 /*#define f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 #define f3(x,y,z) ((x & y) | (z & (x | y))) /* Rounds 40-59 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 #define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 /* SHA constants */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 #define CONST1 0x5a827999L /* Rounds 0-19 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 #define CONST2 0x6ed9eba1L /* Rounds 20-39 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 #define CONST3 0x8f1bbcdcL /* Rounds 40-59 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 #define CONST4 0xca62c1d6L /* Rounds 60-79 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 /* 32-bit rotate */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 #define R32(x,n) ((x << n) | (x >> (32 - n)))
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 /* the generic case, for when the overall rotation is not unraveled */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 #define FG(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 E = D; D = C; C = R32(B,30); B = A; A = T
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 /* specific cases, for when the overall rotation is unraveled */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 #define FA(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 #define FB(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 #define FC(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 #define FD(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 #define FE(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 #define FT(n) \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 /* do SHA transformation */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
162 sha_transform(SHAobject *sha_info)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 int i;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 SHA_INT32 T, A, B, C, D, E, W[80], *WP;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 memcpy(W, sha_info->data, sizeof(sha_info->data));
10562
a42824cf0b18 casts for picky compilers.
Guido van Rossum <guido@python.org>
parents: 10490
diff changeset
168 longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 for (i = 16; i < 80; ++i) {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 /* extra rotation fix */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 W[i] = R32(W[i], 1);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 A = sha_info->digest[0];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 B = sha_info->digest[1];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 C = sha_info->digest[2];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 D = sha_info->digest[3];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 E = sha_info->digest[4];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 WP = W;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 #ifdef UNRAVEL
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 sha_info->digest[0] += E;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 sha_info->digest[1] += T;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 sha_info->digest[2] += A;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 sha_info->digest[3] += B;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 sha_info->digest[4] += C;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 #else /* !UNRAVEL */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 #ifdef UNROLL_LOOPS
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 #else /* !UNROLL_LOOPS */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 for (i = 0; i < 20; ++i) { FG(1); }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 for (i = 20; i < 40; ++i) { FG(2); }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 for (i = 40; i < 60; ++i) { FG(3); }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 for (i = 60; i < 80; ++i) { FG(4); }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 #endif /* !UNROLL_LOOPS */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 sha_info->digest[0] += A;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 sha_info->digest[1] += B;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 sha_info->digest[2] += C;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 sha_info->digest[3] += D;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 sha_info->digest[4] += E;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 #endif /* !UNRAVEL */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 /* initialize the SHA digest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
223 sha_init(SHAobject *sha_info)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 TestEndianness(sha_info->Endianness)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 sha_info->digest[0] = 0x67452301L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 sha_info->digest[1] = 0xefcdab89L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 sha_info->digest[2] = 0x98badcfeL;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 sha_info->digest[3] = 0x10325476L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 sha_info->digest[4] = 0xc3d2e1f0L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 sha_info->count_lo = 0L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 sha_info->count_hi = 0L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 sha_info->local = 0;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 /* update the SHA digest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 static void
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
240 sha_update(SHAobject *sha_info, SHA_BYTE *buffer, unsigned int count)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 {
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
242 unsigned int i;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 SHA_INT32 clo;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 if (clo < sha_info->count_lo) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
247 ++sha_info->count_hi;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 sha_info->count_lo = clo;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 sha_info->count_hi += (SHA_INT32) count >> 29;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 if (sha_info->local) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
252 i = SHA_BLOCKSIZE - sha_info->local;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
253 if (i > count) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
254 i = count;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
255 }
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
256 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
257 count -= i;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
258 buffer += i;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
259 sha_info->local += i;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
260 if (sha_info->local == SHA_BLOCKSIZE) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
261 sha_transform(sha_info);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
262 }
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
263 else {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
264 return;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
265 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 while (count >= SHA_BLOCKSIZE) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
268 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
269 buffer += SHA_BLOCKSIZE;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
270 count -= SHA_BLOCKSIZE;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
271 sha_transform(sha_info);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 memcpy(sha_info->data, buffer, count);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 sha_info->local = count;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 /* finish computing the SHA digest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
280 sha_final(unsigned char digest[20], SHAobject *sha_info)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 int count;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 SHA_INT32 lo_bit_count, hi_bit_count;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 lo_bit_count = sha_info->count_lo;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 hi_bit_count = sha_info->count_hi;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 count = (int) ((lo_bit_count >> 3) & 0x3f);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
289 if (count > SHA_BLOCKSIZE - 8) {
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 memset(((SHA_BYTE *) sha_info->data) + count, 0,
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 SHA_BLOCKSIZE - count);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 sha_transform(sha_info);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 }
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
295 else {
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 memset(((SHA_BYTE *) sha_info->data) + count, 0,
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 SHA_BLOCKSIZE - 8 - count);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 swap these values into host-order. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 sha_transform(sha_info);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 /*
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 * End of copied SHA code.
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 *
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 * ------------------------------------------------------------------------
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
339 static PyTypeObject SHAtype;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 static SHAobject *
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13429
diff changeset
343 newSHAobject(void)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
345 return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 /* Internal methods for a hashing object */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
351 SHA_dealloc(PyObject *ptr)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
353 PyObject_Del(ptr);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 /* External methods for a hashing object */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
359 PyDoc_STRVAR(SHA_copy__doc__, "Return a copy of the hashing object.");
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
362 SHA_copy(SHAobject *self, PyObject *unused)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
364 SHAobject *newobj;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
366 if ( (newobj = newSHAobject())==NULL)
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
367 return NULL;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
369 SHAcopy(self, newobj);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
370 return (PyObject *)newobj;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
373 PyDoc_STRVAR(SHA_digest__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
374 "Return the digest value as a string of binary data.");
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
377 SHA_digest(SHAobject *self, PyObject *unused)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
379 unsigned char digest[SHA_DIGESTSIZE];
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
380 SHAobject temp;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
382 SHAcopy(self, &temp);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
383 sha_final(digest, &temp);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
384 return PyString_FromStringAndSize((const char *)digest, sizeof(digest));
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
387 PyDoc_STRVAR(SHA_hexdigest__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
388 "Return the digest value as a string of hexadecimal digits.");
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
391 SHA_hexdigest(SHAobject *self, PyObject *unused)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
393 unsigned char digest[SHA_DIGESTSIZE];
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
394 SHAobject temp;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
395 PyObject *retval;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
396 char *hex_digest;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
397 int i, j;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
399 /* Get the raw (binary) digest value */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
400 SHAcopy(self, &temp);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
401 sha_final(digest, &temp);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
403 /* Create a new string */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
404 retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
13982
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
405 if (!retval)
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
406 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
407 hex_digest = PyString_AsString(retval);
13982
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
408 if (!hex_digest) {
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
409 Py_DECREF(retval);
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
410 return NULL;
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
411 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
413 /* Make hex version of the digest */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
414 for(i=j=0; i<sizeof(digest); i++) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
415 char c;
13982
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
416 c = (digest[i] >> 4) & 0xf;
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
417 c = (c>9) ? c+'a'-10 : c + '0';
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
418 hex_digest[j++] = c;
13982
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
419 c = (digest[i] & 0xf);
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
420 c = (c>9) ? c+'a'-10 : c + '0';
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
421 hex_digest[j++] = c;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
422 }
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
423 return retval;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
426 PyDoc_STRVAR(SHA_update__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
427 "Update this hashing object's state with the provided string.");
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 static PyObject *
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
430 SHA_update(SHAobject *self, PyObject *args)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 {
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
432 PyObject *data_obj;
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
433 Py_buffer view;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
435 if (!PyArg_ParseTuple(args, "O:update", &data_obj))
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
436 return NULL;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
438 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
440 sha_update(self, (unsigned char*)view.buf,
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
441 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
442
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
443 PyBuffer_Release(&view);
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
444 Py_INCREF(Py_None);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
445 return Py_None;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 static PyMethodDef SHA_methods[] = {
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
449 {"copy", (PyCFunction)SHA_copy, METH_NOARGS, SHA_copy__doc__},
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
450 {"digest", (PyCFunction)SHA_digest, METH_NOARGS, SHA_digest__doc__},
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
451 {"hexdigest", (PyCFunction)SHA_hexdigest, METH_NOARGS, SHA_hexdigest__doc__},
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
452 {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__},
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
453 {NULL, NULL} /* sentinel */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 };
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 static PyObject *
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
457 SHA_get_block_size(PyObject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
458 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
459 return PyInt_FromLong(SHA_BLOCKSIZE);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
460 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
461
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
462 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
463 SHA_get_digest_size(PyObject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
464 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
465 return PyInt_FromLong(SHA_DIGESTSIZE);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
466 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
467
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
468 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
469 SHA_get_name(PyObject *self, void *closure)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
471 return PyString_FromStringAndSize("SHA1", 4);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
472 }
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
473
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
474 static PyGetSetDef SHA_getseters[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
475 {"digest_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
476 (getter)SHA_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
477 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
478 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
479 {"block_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
480 (getter)SHA_get_block_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
481 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
482 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
483 {"name",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
484 (getter)SHA_get_name, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
485 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
486 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
487 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
488 * the old sha module also supported 'digestsize'. ugh. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
489 {"digestsize",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
490 (getter)SHA_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
491 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
492 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
493 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
494 };
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 static PyTypeObject SHAtype = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
497 PyVarObject_HEAD_INIT(NULL, 0)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
498 "_sha.sha", /*tp_name*/
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
499 sizeof(SHAobject), /*tp_size*/
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
500 0, /*tp_itemsize*/
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
501 /* methods */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
502 SHA_dealloc, /*tp_dealloc*/
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
503 0, /*tp_print*/
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
504 0, /*tp_getattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
505 0, /*tp_setattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
506 0, /*tp_compare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
507 0, /*tp_repr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
508 0, /*tp_as_number*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
509 0, /*tp_as_sequence*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
510 0, /*tp_as_mapping*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
511 0, /*tp_hash*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
512 0, /*tp_call*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
513 0, /*tp_str*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
514 0, /*tp_getattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
515 0, /*tp_setattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
516 0, /*tp_as_buffer*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
517 Py_TPFLAGS_DEFAULT, /*tp_flags*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
518 0, /*tp_doc*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
519 0, /*tp_traverse*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
520 0, /*tp_clear*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
521 0, /*tp_richcompare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
522 0, /*tp_weaklistoffset*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
523 0, /*tp_iter*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
524 0, /*tp_iternext*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
525 SHA_methods, /* tp_methods */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
526 0, /* tp_members */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
527 SHA_getseters, /* tp_getset */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 };
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
529
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
530
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 /* The single module-level function: new() */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
533 PyDoc_STRVAR(SHA_new__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
534 "Return a new SHA hashing object. An optional string argument\n\
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
535 may be provided; if present, this string will be automatically\n\
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
536 hashed.");
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 static PyObject *
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
539 SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
541 static char *kwlist[] = {"string", NULL};
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
542 SHAobject *new;
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
543 PyObject *data_obj = NULL;
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
544 Py_buffer view;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
546 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
547 &data_obj)) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
548 return NULL;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
549 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
550
51961
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
551 if (data_obj)
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
552 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
553
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
554 if ((new = newSHAobject()) == NULL) {
51961
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
555 if (data_obj)
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
556 PyBuffer_Release(&view);
16354
c3b75a7fad3f Cleanup logic a little. Check args first, then try to create the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15803
diff changeset
557 return NULL;
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
558 }
16354
c3b75a7fad3f Cleanup logic a little. Check args first, then try to create the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15803
diff changeset
559
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
560 sha_init(new);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
562 if (PyErr_Occurred()) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
563 Py_DECREF(new);
51961
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
564 if (data_obj)
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
565 PyBuffer_Release(&view);
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
566 return NULL;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
567 }
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
568 if (data_obj) {
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
569 sha_update(new, (unsigned char*)view.buf,
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
570 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
51961
6c08bfe78fff Fix SHA_new and MD5_new, that would crash if not given initial data
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 51580
diff changeset
571 PyBuffer_Release(&view);
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
572 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
573
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
574 return (PyObject *)new;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 /* List of functions exported by this module */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 static struct PyMethodDef SHA_functions[] = {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
581 {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
582 {NULL, NULL} /* Sentinel */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 };
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 /* Initialize this module. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587
20849
404aeeca51e9 Clean up a Tab inconsistency.
Fred Drake <fdrake@acm.org>
parents: 20843
diff changeset
588 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
589
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
590 PyMODINIT_FUNC
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
591 init_sha(void)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 {
22727
0973693e4ad2 Remove unused variable and call to PyModule_GetDict().
Fred Drake <fdrake@acm.org>
parents: 21313
diff changeset
593 PyObject *m;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43298
diff changeset
595 Py_TYPE(&SHAtype) = &PyType_Type;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
596 if (PyType_Ready(&SHAtype) < 0)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
597 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
598 m = Py_InitModule("_sha", SHA_functions);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35375
diff changeset
599 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35375
diff changeset
600 return;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
602 /* Add some symbolic constants to the module */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
603 insint("blocksize", 1); /* For future use, in case some hash
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
604 functions require an integral number of
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
605 blocks */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
606 insint("digestsize", 20);
20843
8d14f534a5c2 [Patch #476612] Add attributes from PEP247 to the md5 and sha modules
Andrew M. Kuchling <amk@amk.ca>
parents: 16354
diff changeset
607 insint("digest_size", 20);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 }