annotate Modules/shamodule.c @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 4d36e56b56d9
children
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"
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
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 /* Endianness testing and definitions */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
24 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 #define PCT_LITTLE_ENDIAN 1
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #define PCT_BIG_ENDIAN 0
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 /* Some useful types */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 typedef unsigned char SHA_BYTE;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #if SIZEOF_INT == 4
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
34 typedef unsigned int SHA_INT32; /* 32-bit integer */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #else
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 /* not defined. compilation will die. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #endif
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 /* 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
40
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #define SHA_BLOCKSIZE 64
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 #define SHA_DIGESTSIZE 20
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 /* 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
45
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 typedef struct {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
47 PyObject_HEAD
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
48 SHA_INT32 digest[5]; /* Message digest */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
49 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
50 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
51 int Endianness;
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
52 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
53 } SHAobject;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 /* 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
56 array of longwords. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
58 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
59 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 SHA_INT32 value;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if ( Endianness == PCT_BIG_ENDIAN )
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
63 return;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 byteCount /= sizeof(*buffer);
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
66 while (byteCount--) {
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 value = *buffer;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 ( ( value & 0x00FF00FFL ) << 8 );
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 *buffer++ = ( value << 16 ) | ( value >> 16 );
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 }
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
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
74 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
75 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
76 dest->Endianness = src->Endianness;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
77 dest->local = src->local;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
78 dest->count_lo = src->count_lo;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
79 dest->count_hi = src->count_hi;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
80 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
81 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
82 }
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 * 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
88 * headers are pasted below.
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 *
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 * 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
91 * Python environment and desired interface.
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 *
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 /* NIST Secure Hash Algorithm */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 /* 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
97 /* 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
98 /* Applied Cryptography by Bruce Schneier */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 /* 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
100
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 /* 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
102
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 /* UNRAVEL should be fastest & biggest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 /* 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
105 /* 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
106
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 #define UNRAVEL
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 /* #define UNROLL_LOOPS */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 /* 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
111 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
112 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
113
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
114 /*#define f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
115 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
116 #define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
117 /*#define f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
118 #define f3(x,y,z) ((x & y) | (z & (x | y))) /* Rounds 40-59 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
119 #define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 /* SHA constants */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
123 #define CONST1 0x5a827999L /* Rounds 0-19 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
124 #define CONST2 0x6ed9eba1L /* Rounds 20-39 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
125 #define CONST3 0x8f1bbcdcL /* Rounds 40-59 */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
126 #define CONST4 0xca62c1d6L /* Rounds 60-79 */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 /* 32-bit rotate */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
130 #define R32(x,n) ((x << n) | (x >> (32 - n)))
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 /* 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
133
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
134 #define FG(n) \
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
135 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 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
137
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 /* 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
139
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
140 #define FA(n) \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
143 #define FB(n) \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 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
145
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
146 #define FC(n) \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 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
148
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
149 #define FD(n) \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 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
151
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
152 #define FE(n) \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 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
154
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
155 #define FT(n) \
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 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
157
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 /* do SHA transformation */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
161 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
162 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 int i;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 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
165
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 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
167 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
168
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 for (i = 16; i < 80; ++i) {
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
170 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
172 /* extra rotation fix */
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
173 W[i] = R32(W[i], 1);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 A = sha_info->digest[0];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 B = sha_info->digest[1];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 C = sha_info->digest[2];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 D = sha_info->digest[3];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 E = sha_info->digest[4];
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 WP = W;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 #ifdef UNRAVEL
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 sha_info->digest[0] += E;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 sha_info->digest[1] += T;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 sha_info->digest[2] += A;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 sha_info->digest[3] += B;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 sha_info->digest[4] += C;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 #else /* !UNRAVEL */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 #ifdef UNROLL_LOOPS
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 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
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(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
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(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
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(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
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 #else /* !UNROLL_LOOPS */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 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
207 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
208 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
209 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
210 #endif /* !UNROLL_LOOPS */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 sha_info->digest[0] += A;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 sha_info->digest[1] += B;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 sha_info->digest[2] += C;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 sha_info->digest[3] += D;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 sha_info->digest[4] += E;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 #endif /* !UNRAVEL */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 }
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 /* initialize the SHA digest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
222 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
223 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 TestEndianness(sha_info->Endianness)
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 sha_info->digest[0] = 0x67452301L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 sha_info->digest[1] = 0xefcdab89L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 sha_info->digest[2] = 0x98badcfeL;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 sha_info->digest[3] = 0x10325476L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 sha_info->digest[4] = 0xc3d2e1f0L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 sha_info->count_lo = 0L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 sha_info->count_hi = 0L;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 sha_info->local = 0;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 }
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 /* update the SHA digest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 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
239 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
240 {
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
241 unsigned int i;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 SHA_INT32 clo;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 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
245 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
246 ++sha_info->count_hi;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 sha_info->count_lo = clo;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 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
250 if (sha_info->local) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
251 i = SHA_BLOCKSIZE - sha_info->local;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
252 if (i > count) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
253 i = count;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
254 }
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
255 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
256 count -= i;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
257 buffer += i;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
258 sha_info->local += i;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
259 if (sha_info->local == SHA_BLOCKSIZE) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
260 sha_transform(sha_info);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
261 }
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
262 else {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
263 return;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
264 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 while (count >= SHA_BLOCKSIZE) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
267 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
268 buffer += SHA_BLOCKSIZE;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
269 count -= SHA_BLOCKSIZE;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
270 sha_transform(sha_info);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 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
273 sha_info->local = count;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 }
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 /* finish computing the SHA digest */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
279 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
280 {
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 int count;
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 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
283
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 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
285 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
286 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
287 ((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
288 if (count > SHA_BLOCKSIZE - 8) {
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
289 memset(((SHA_BYTE *) sha_info->data) + count, 0,
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
290 SHA_BLOCKSIZE - count);
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
291 sha_transform(sha_info);
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
292 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 }
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
294 else {
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
295 memset(((SHA_BYTE *) sha_info->data) + count, 0,
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
296 SHA_BLOCKSIZE - 8 - count);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 }
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 /* 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
300 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
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 sha_transform(sha_info);
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
323 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
324 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
325 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
326 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
327 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
328 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
329 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
330 }
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 * End of copied SHA code.
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 *
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
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
338 static PyTypeObject SHAtype;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
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 static SHAobject *
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13429
diff changeset
342 newSHAobject(void)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
344 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
345 }
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 /* 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
348
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 static void
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
350 SHA_dealloc(PyObject *ptr)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
352 PyObject_Del(ptr);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 }
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 /* 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
357
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
358 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
359
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
361 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
362 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
363 SHAobject *newobj;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
365 if ( (newobj = newSHAobject())==NULL)
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
366 return NULL;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
368 SHAcopy(self, newobj);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
369 return (PyObject *)newobj;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
372 PyDoc_STRVAR(SHA_digest__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
373 "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
374
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
376 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
377 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
378 unsigned char digest[SHA_DIGESTSIZE];
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
379 SHAobject temp;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
381 SHAcopy(self, &temp);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
382 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
383 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
384 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
386 PyDoc_STRVAR(SHA_hexdigest__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
387 "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
388
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
390 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
391 {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
392 unsigned char digest[SHA_DIGESTSIZE];
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
393 SHAobject temp;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
394 PyObject *retval;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
395 char *hex_digest;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
396 int i, j;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
398 /* Get the raw (binary) digest value */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
399 SHAcopy(self, &temp);
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
400 sha_final(digest, &temp);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
402 /* 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
403 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
404 if (!retval)
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
405 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
406 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
407 if (!hex_digest) {
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
408 Py_DECREF(retval);
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
409 return NULL;
13982
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
410 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
412 /* Make hex version of the digest */
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
413 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
414 char c;
13982
43f4f8d15971 SHA_hexdigest(): A couple of small patches to this function, added
Barry Warsaw <barry@python.org>
parents: 13782
diff changeset
415 c = (digest[i] >> 4) & 0xf;
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
416 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
417 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
418 c = (digest[i] & 0xf);
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
419 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
420 hex_digest[j++] = c;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
421 }
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
422 return retval;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
425 PyDoc_STRVAR(SHA_update__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
426 "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
427
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 static PyObject *
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
429 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
430 {
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
431 Py_buffer view;
78975
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
432 Py_ssize_t n;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
433 unsigned char *buf;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 51961
diff changeset
435 if (!PyArg_ParseTuple(args, "s*:update", &view))
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
78975
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
438 n = view.len;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
439 buf = (unsigned char *) view.buf;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
440 while (n > 0) {
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
441 Py_ssize_t nbytes;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
442 if (n > INT_MAX)
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
443 nbytes = INT_MAX;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
444 else
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
445 nbytes = n;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
446 sha_update(self, buf,
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
447 Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
448 buf += nbytes;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
449 n -= nbytes;
4d36e56b56d9 Closes #15910: MD5 and SHA1 crash when "updated" with strings bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 78974
diff changeset
450 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
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
452 PyBuffer_Release(&view);
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 51961
diff changeset
453 Py_RETURN_NONE;
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 PyMethodDef SHA_methods[] = {
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
457 {"copy", (PyCFunction)SHA_copy, METH_NOARGS, SHA_copy__doc__},
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
458 {"digest", (PyCFunction)SHA_digest, METH_NOARGS, SHA_digest__doc__},
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36675
diff changeset
459 {"hexdigest", (PyCFunction)SHA_hexdigest, METH_NOARGS, SHA_hexdigest__doc__},
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
460 {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__},
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
461 {NULL, NULL} /* sentinel */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 };
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 static PyObject *
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
465 SHA_get_block_size(PyObject *self, void *closure)
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 return PyInt_FromLong(SHA_BLOCKSIZE);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
468 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
469
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
470 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
471 SHA_get_digest_size(PyObject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
472 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
473 return PyInt_FromLong(SHA_DIGESTSIZE);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
474 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
475
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
476 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
477 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
478 {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
479 return PyString_FromStringAndSize("SHA1", 4);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
480 }
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
481
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
482 static PyGetSetDef SHA_getseters[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
483 {"digest_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
484 (getter)SHA_get_digest_size, 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 {"block_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
488 (getter)SHA_get_block_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
489 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
490 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
491 {"name",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
492 (getter)SHA_get_name, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
493 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
494 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
495 /* 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
496 * the old sha module also supported 'digestsize'. ugh. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
497 {"digestsize",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
498 (getter)SHA_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
499 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
500 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
501 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
502 };
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 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
505 PyVarObject_HEAD_INIT(NULL, 0)
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
506 "_sha.sha", /*tp_name*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
507 sizeof(SHAobject), /*tp_size*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
508 0, /*tp_itemsize*/
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
509 /* methods */
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
510 SHA_dealloc, /*tp_dealloc*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
511 0, /*tp_print*/
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
512 0, /*tp_getattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
513 0, /*tp_setattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
514 0, /*tp_compare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
515 0, /*tp_repr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
516 0, /*tp_as_number*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
517 0, /*tp_as_sequence*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
518 0, /*tp_as_mapping*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
519 0, /*tp_hash*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
520 0, /*tp_call*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
521 0, /*tp_str*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
522 0, /*tp_getattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
523 0, /*tp_setattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
524 0, /*tp_as_buffer*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
525 Py_TPFLAGS_DEFAULT, /*tp_flags*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
526 0, /*tp_doc*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
527 0, /*tp_traverse*/
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
528 0, /*tp_clear*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
529 0, /*tp_richcompare*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
530 0, /*tp_weaklistoffset*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
531 0, /*tp_iter*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
532 0, /*tp_iternext*/
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
533 SHA_methods, /* tp_methods */
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
534 0, /* tp_members */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
535 SHA_getseters, /* tp_getset */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 };
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
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 /* 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
540
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
541 PyDoc_STRVAR(SHA_new__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22727
diff changeset
542 "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
543 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
544 hashed.");
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
545
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 static PyObject *
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
547 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
548 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
549 static char *kwlist[] = {"string", NULL};
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
550 SHAobject *new;
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 51961
diff changeset
551 Py_buffer view = { 0 };
78974
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
552 Py_ssize_t n;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
553 unsigned char *buf;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
554
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 51961
diff changeset
555 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 51961
diff changeset
556 &view)) {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
557 return NULL;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
558 }
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
559
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
560 if ((new = newSHAobject()) == NULL) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58101
diff changeset
561 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
562 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
563 }
16354
c3b75a7fad3f Cleanup logic a little. Check args first, then try to create the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15803
diff changeset
564
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
565 sha_init(new);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
567 if (PyErr_Occurred()) {
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
568 Py_DECREF(new);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58101
diff changeset
569 PyBuffer_Release(&view);
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
570 return NULL;
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
571 }
78974
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
572
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
573 n = view.len;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
574 buf = (unsigned char *) view.buf;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
575 while (n > 0) {
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
576 Py_ssize_t nbytes;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
577 if (n > INT_MAX)
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
578 nbytes = INT_MAX;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
579 else
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
580 nbytes = n;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
581 sha_update(new, buf,
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
582 Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int));
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
583 buf += nbytes;
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
584 n -= nbytes;
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
585 }
78974
a13bfc238f27 Closes #15908: SHA1 crashes in 64 bits when the string to hash is bigger than 2**32 bytes
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
586
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 51961
diff changeset
587 PyBuffer_Release(&view);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
589 return (PyObject *)new;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 }
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
591
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 /* 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
594
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 static struct PyMethodDef SHA_functions[] = {
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
596 {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
597 {NULL, NULL} /* Sentinel */
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 };
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 /* Initialize this module. */
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
602
20849
404aeeca51e9 Clean up a Tab inconsistency.
Fred Drake <fdrake@acm.org>
parents: 20843
diff changeset
603 #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
604
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
605 PyMODINIT_FUNC
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
606 init_sha(void)
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 {
22727
0973693e4ad2 Remove unused variable and call to PyModule_GetDict().
Fred Drake <fdrake@acm.org>
parents: 21313
diff changeset
608 PyObject *m;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609
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
610 Py_TYPE(&SHAtype) = &PyType_Type;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
611 if (PyType_Ready(&SHAtype) < 0)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
612 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents: 25966
diff changeset
613 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
614 if (m == NULL)
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
615 return;
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
616
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
617 /* 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
618 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
619 functions require an integral number of
61465
e38335d4df91 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 47527
diff changeset
620 blocks */
13429
8a3d50ed06f7 ANSI-fied sources, converted to four-space indentation.
Fred Drake <fdrake@acm.org>
parents: 13255
diff changeset
621 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
622 insint("digest_size", 20);
10458
70910e3125a2 Added Greg Stein and Andrew Kuchling's sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 }