annotate Modules/sha1module.c @ 71285:8bc9dbc61ba6 3.2

Issue #12326: document the recommended idiom for checking sys.platform on Unix systems. Also, point to the various alternatives.
author Antoine Pitrou <solipsis@pitrou.net>
date Sat, 09 Jul 2011 15:54:23 +0200
parents 3bf4d9f1660a
children 43fd627cc060
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 /* SHA1 module */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 /* This module provides an interface to the SHA1 algorithm */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
5 /* See below for information about the original code this module was
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 based upon. Additional work performed by:
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
7
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
8 Andrew Kuchling (amk@amk.ca)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
9 Greg Stein (gstein@lyra.org)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 Trevor Perrin (trevp@trevp.net)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
11
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
12 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
13 Licensed to PSF under a Contributor Agreement.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
14
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
15 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
16
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17 /* SHA1 objects */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
18
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19 #include "Python.h"
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
20 #include "hashlib.h"
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
22
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
23 /* Some useful types */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
25 #if SIZEOF_INT == 4
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
26 typedef unsigned int SHA1_INT32; /* 32-bit integer */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
27 typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
28 #else
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
29 /* not defined. compilation will die. */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
30 #endif
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
31
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
32 /* The SHA1 block size and message digest sizes, in bytes */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
34 #define SHA1_BLOCKSIZE 64
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
35 #define SHA1_DIGESTSIZE 20
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37 /* The structure for storing SHA1 info */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39 struct sha1_state {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40 SHA1_INT64 length;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
41 SHA1_INT32 state[5], curlen;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
42 unsigned char buf[SHA1_BLOCKSIZE];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
43 };
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
44
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
45 typedef struct {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
46 PyObject_HEAD
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
47
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
48 struct sha1_state hash_state;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
49 } SHA1object;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
50
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
51
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
52 /* ------------------------------------------------------------------------
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
53 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54 * This code for the SHA1 algorithm was noted as public domain. The
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
55 * original headers are pasted below.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
57 * Several changes have been made to make it more compatible with the
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
58 * Python environment and desired interface.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
59 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
60 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
61
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
62 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
64 * LibTomCrypt is a library that provides various cryptographic
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
65 * algorithms in a highly modular and flexible manner.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
66 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
67 * The library is free for all purposes without any express
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
68 * guarantee it works.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
70 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
71 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
72
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
73 /* rotate the hard way (platform optimizations could be done) */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
74 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
75 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
76
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
77 /* Endian Neutral macros that work on all platforms */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
78
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
79 #define STORE32H(x, y) \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
80 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
81 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
82
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
83 #define LOAD32H(x, y) \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
84 { x = ((unsigned long)((y)[0] & 255)<<24) | \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85 ((unsigned long)((y)[1] & 255)<<16) | \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
86 ((unsigned long)((y)[2] & 255)<<8) | \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 ((unsigned long)((y)[3] & 255)); }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
88
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
89 #define STORE64H(x, y) \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
90 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
91 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
92 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
93 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
94
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
95 #ifndef MIN
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
96 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
97 #endif
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
98
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
99
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
100 /* SHA1 macros */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
101
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
102 #define F0(x,y,z) (z ^ (x & (y ^ z)))
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
103 #define F1(x,y,z) (x ^ y ^ z)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
104 #define F2(x,y,z) ((x & y) | (z & (x | y)))
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
105 #define F3(x,y,z) (x ^ y ^ z)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
106
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
107 static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
108 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
109 SHA1_INT32 a,b,c,d,e,W[80],i;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
110
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
111 /* copy the state into 512-bits into W[0..15] */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
112 for (i = 0; i < 16; i++) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
113 LOAD32H(W[i], buf + (4*i));
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
114 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
115
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
116 /* copy state */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 a = sha1->state[0];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118 b = sha1->state[1];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
119 c = sha1->state[2];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120 d = sha1->state[3];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
121 e = sha1->state[4];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
122
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
123 /* expand it */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
124 for (i = 16; i < 80; i++) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
125 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
126 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
127
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
128 /* compress */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
129 /* round one */
63164
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
130 #define FF_0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
131 #define FF_1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
132 #define FF_2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
133 #define FF_3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
134
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
135 for (i = 0; i < 20; ) {
63164
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
136 FF_0(a,b,c,d,e,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
137 FF_0(e,a,b,c,d,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
138 FF_0(d,e,a,b,c,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
139 FF_0(c,d,e,a,b,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
140 FF_0(b,c,d,e,a,i++);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
142
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
143 /* round two */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
144 for (; i < 40; ) {
63164
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
145 FF_1(a,b,c,d,e,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
146 FF_1(e,a,b,c,d,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
147 FF_1(d,e,a,b,c,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
148 FF_1(c,d,e,a,b,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
149 FF_1(b,c,d,e,a,i++);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
151
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
152 /* round three */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
153 for (; i < 60; ) {
63164
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
154 FF_2(a,b,c,d,e,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
155 FF_2(e,a,b,c,d,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
156 FF_2(d,e,a,b,c,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
157 FF_2(c,d,e,a,b,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
158 FF_2(b,c,d,e,a,i++);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
159 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
160
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161 /* round four */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
162 for (; i < 80; ) {
63164
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
163 FF_3(a,b,c,d,e,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
164 FF_3(e,a,b,c,d,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
165 FF_3(d,e,a,b,c,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
166 FF_3(c,d,e,a,b,i++);
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
167 FF_3(b,c,d,e,a,i++);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
168 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
169
63164
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
170 #undef FF_0
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
171 #undef FF_1
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
172 #undef FF_2
2855d6363192 Rename some macros in the sha1 module to no longer conflict with termios.h.
Brett Cannon <bcannon@gmail.com>
parents: 61466
diff changeset
173 #undef FF_3
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
174
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
175 /* store */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
176 sha1->state[0] = sha1->state[0] + a;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
177 sha1->state[1] = sha1->state[1] + b;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
178 sha1->state[2] = sha1->state[2] + c;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
179 sha1->state[3] = sha1->state[3] + d;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
180 sha1->state[4] = sha1->state[4] + e;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
181 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
182
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
183 /**
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
184 Initialize the hash state
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
185 @param sha1 The hash state you wish to initialize
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
186 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
187 void sha1_init(struct sha1_state *sha1)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
188 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
189 assert(sha1 != NULL);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190 sha1->state[0] = 0x67452301UL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
191 sha1->state[1] = 0xefcdab89UL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
192 sha1->state[2] = 0x98badcfeUL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
193 sha1->state[3] = 0x10325476UL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
194 sha1->state[4] = 0xc3d2e1f0UL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
195 sha1->curlen = 0;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
196 sha1->length = 0;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
197 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
199 /**
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
200 Process a block of memory though the hash
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201 @param sha1 The hash state
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202 @param in The data to hash
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
203 @param inlen The length of the data (octets)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
204 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205 void sha1_process(struct sha1_state *sha1,
67347
3bf4d9f1660a Issue #9566: use Py_ssize_t instead of int
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63164
diff changeset
206 const unsigned char *in, Py_ssize_t inlen)
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 {
67347
3bf4d9f1660a Issue #9566: use Py_ssize_t instead of int
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63164
diff changeset
208 Py_ssize_t n;
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
210 assert(sha1 != NULL);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
211 assert(in != NULL);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
212 assert(sha1->curlen <= sizeof(sha1->buf));
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
213
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
214 while (inlen > 0) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
215 if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
216 sha1_compress(sha1, (unsigned char *)in);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
217 sha1->length += SHA1_BLOCKSIZE * 8;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
218 in += SHA1_BLOCKSIZE;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
219 inlen -= SHA1_BLOCKSIZE;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
220 } else {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
221 n = MIN(inlen, (SHA1_BLOCKSIZE - sha1->curlen));
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
222 memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
223 sha1->curlen += n;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
224 in += n;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
225 inlen -= n;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
226 if (sha1->curlen == SHA1_BLOCKSIZE) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
227 sha1_compress(sha1, sha1->buf);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
228 sha1->length += 8*SHA1_BLOCKSIZE;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
229 sha1->curlen = 0;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
230 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
231 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
232 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
233 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
234
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
235 /**
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
236 Terminate the hash to get the digest
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
237 @param sha1 The hash state
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
238 @param out [out] The destination of the hash (20 bytes)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
239 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
240 void sha1_done(struct sha1_state *sha1, unsigned char *out)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
242 int i;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
243
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
244 assert(sha1 != NULL);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
245 assert(out != NULL);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
246 assert(sha1->curlen < sizeof(sha1->buf));
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
247
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
248 /* increase the length of the message */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
249 sha1->length += sha1->curlen * 8;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
251 /* append the '1' bit */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
252 sha1->buf[sha1->curlen++] = (unsigned char)0x80;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
253
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
254 /* if the length is currently above 56 bytes we append zeros
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
255 * then compress. Then we can fall back to padding zeros and length
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
256 * encoding like normal.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
257 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
258 if (sha1->curlen > 56) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
259 while (sha1->curlen < 64) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
260 sha1->buf[sha1->curlen++] = (unsigned char)0;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
261 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
262 sha1_compress(sha1, sha1->buf);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263 sha1->curlen = 0;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
264 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
265
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
266 /* pad upto 56 bytes of zeroes */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
267 while (sha1->curlen < 56) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
268 sha1->buf[sha1->curlen++] = (unsigned char)0;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
269 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
270
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271 /* store length */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
272 STORE64H(sha1->length, sha1->buf+56);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
273 sha1_compress(sha1, sha1->buf);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
274
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
275 /* copy output */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
276 for (i = 0; i < 5; i++) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
277 STORE32H(sha1->state[i], out+(4*i));
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
278 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
279 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
280
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
281
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
282 /* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
283 /* .Revision: 1.10 $ */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284 /* .Date: 2007/05/12 14:25:28 $ */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286 /*
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287 * End of copied SHA1 code.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
288 *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
289 * ------------------------------------------------------------------------
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290 */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
291
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
292 static PyTypeObject SHA1type;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
293
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
295 static SHA1object *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
296 newSHA1object(void)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
298 return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
299 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
300
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
301
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302 /* Internal methods for a hash object */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
304 static void
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
305 SHA1_dealloc(PyObject *ptr)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
306 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307 PyObject_Del(ptr);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
308 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311 /* External methods for a hash object */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
313 PyDoc_STRVAR(SHA1_copy__doc__, "Return a copy of the hash object.");
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
315 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
316 SHA1_copy(SHA1object *self, PyObject *unused)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
317 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
318 SHA1object *newobj;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
320 if (Py_TYPE(self) == &SHA1type) {
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
321 if ( (newobj = newSHA1object())==NULL)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
322 return NULL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
323 } else {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
324 if ( (newobj = newSHA1object())==NULL)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
325 return NULL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
326 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
327
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
328 newobj->hash_state = self->hash_state;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
329 return (PyObject *)newobj;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
330 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
332 PyDoc_STRVAR(SHA1_digest__doc__,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
333 "Return the digest value as a string of binary data.");
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
336 SHA1_digest(SHA1object *self, PyObject *unused)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
338 unsigned char digest[SHA1_DIGESTSIZE];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
339 struct sha1_state temp;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 temp = self->hash_state;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
342 sha1_done(&temp, digest);
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
343 return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
344 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
345
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
346 PyDoc_STRVAR(SHA1_hexdigest__doc__,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
347 "Return the digest value as a string of hexadecimal digits.");
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
349 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
350 SHA1_hexdigest(SHA1object *self, PyObject *unused)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
351 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
352 unsigned char digest[SHA1_DIGESTSIZE];
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
353 struct sha1_state temp;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
354 PyObject *retval;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
355 Py_UNICODE *hex_digest;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
356 int i, j;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
357
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
358 /* Get the raw (binary) digest value */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
359 temp = self->hash_state;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
360 sha1_done(&temp, digest);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
361
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
362 /* Create a new string */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
363 retval = PyUnicode_FromStringAndSize(NULL, SHA1_DIGESTSIZE * 2);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
364 if (!retval)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
365 return NULL;
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
366 hex_digest = PyUnicode_AS_UNICODE(retval);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
367 if (!hex_digest) {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
368 Py_DECREF(retval);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
369 return NULL;
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
370 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
371
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
372 /* Make hex version of the digest */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
373 for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
374 char c;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
375 c = (digest[i] >> 4) & 0xf;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
376 c = (c>9) ? c+'a'-10 : c + '0';
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
377 hex_digest[j++] = c;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
378 c = (digest[i] & 0xf);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
379 c = (c>9) ? c+'a'-10 : c + '0';
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
380 hex_digest[j++] = c;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
381 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
382 return retval;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
383 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
384
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 PyDoc_STRVAR(SHA1_update__doc__,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
386 "Update this hash object's state with the provided string.");
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
387
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
388 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
389 SHA1_update(SHA1object *self, PyObject *args)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
390 {
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
391 PyObject *obj;
48691
ade33e13ec69 Use s* to receive data. Fixes #3552.
Martin v. Löwis <martin@v.loewis.de>
parents: 47581
diff changeset
392 Py_buffer buf;
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
393
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
394 if (!PyArg_ParseTuple(args, "O:update", &obj))
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 return NULL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
397 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
398
48691
ade33e13ec69 Use s* to receive data. Fixes #3552.
Martin v. Löwis <martin@v.loewis.de>
parents: 47581
diff changeset
399 sha1_process(&self->hash_state, buf.buf, buf.len);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
400
48691
ade33e13ec69 Use s* to receive data. Fixes #3552.
Martin v. Löwis <martin@v.loewis.de>
parents: 47581
diff changeset
401 PyBuffer_Release(&buf);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
402 Py_INCREF(Py_None);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
403 return Py_None;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
404 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
405
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
406 static PyMethodDef SHA1_methods[] = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
407 {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__},
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
408 {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__},
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
409 {"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS, SHA1_hexdigest__doc__},
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
410 {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__},
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
411 {NULL, NULL} /* sentinel */
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
412 };
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
414 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
415 SHA1_get_block_size(PyObject *self, void *closure)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
416 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
417 return PyLong_FromLong(SHA1_BLOCKSIZE);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
418 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
419
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
420 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
421 SHA1_get_name(PyObject *self, void *closure)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
423 return PyUnicode_FromStringAndSize("SHA1", 3);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
424 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
425
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
426 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
427 sha1_get_digest_size(PyObject *self, void *closure)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
428 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
429 return PyLong_FromLong(SHA1_DIGESTSIZE);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
430 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
431
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
432
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
433 static PyGetSetDef SHA1_getseters[] = {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
434 {"block_size",
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
435 (getter)SHA1_get_block_size, NULL,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
436 NULL,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
437 NULL},
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
438 {"name",
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
439 (getter)SHA1_get_name, NULL,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
440 NULL,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
441 NULL},
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
442 {"digest_size",
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
443 (getter)sha1_get_digest_size, NULL,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
444 NULL,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
445 NULL},
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
446 {NULL} /* Sentinel */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
447 };
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
448
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
449 static PyTypeObject SHA1type = {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
450 PyVarObject_HEAD_INIT(NULL, 0)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
451 "_sha1.sha1", /*tp_name*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
452 sizeof(SHA1object), /*tp_size*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
453 0, /*tp_itemsize*/
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
454 /* methods */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
455 SHA1_dealloc, /*tp_dealloc*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
456 0, /*tp_print*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
457 0, /*tp_getattr*/
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
458 0, /*tp_setattr*/
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48697
diff changeset
459 0, /*tp_reserved*/
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
460 0, /*tp_repr*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
461 0, /*tp_as_number*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
462 0, /*tp_as_sequence*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
463 0, /*tp_as_mapping*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
464 0, /*tp_hash*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
465 0, /*tp_call*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
466 0, /*tp_str*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
467 0, /*tp_getattro*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
468 0, /*tp_setattro*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
469 0, /*tp_as_buffer*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
470 Py_TPFLAGS_DEFAULT, /*tp_flags*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
471 0, /*tp_doc*/
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
472 0, /*tp_traverse*/
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
473 0, /*tp_clear*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
474 0, /*tp_richcompare*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
475 0, /*tp_weaklistoffset*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
476 0, /*tp_iter*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
477 0, /*tp_iternext*/
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
478 SHA1_methods, /* tp_methods */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
479 NULL, /* tp_members */
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
480 SHA1_getseters, /* tp_getset */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
481 };
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
482
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
483
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
484 /* The single module-level function: new() */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
485
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486 PyDoc_STRVAR(SHA1_new__doc__,
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
487 "Return a new SHA1 hash object; optionally initialized with a string.");
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
488
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
489 static PyObject *
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
490 SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict)
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
491 {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
492 static char *kwlist[] = {"string", NULL};
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
493 SHA1object *new;
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
494 PyObject *data_obj = NULL;
48691
ade33e13ec69 Use s* to receive data. Fixes #3552.
Martin v. Löwis <martin@v.loewis.de>
parents: 47581
diff changeset
495 Py_buffer buf;
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
496
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
497 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
498 &data_obj)) {
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
499 return NULL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
500 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
502 if (data_obj)
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
503 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
504
51969
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
505 if ((new = newSHA1object()) == NULL) {
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
506 if (data_obj)
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
507 PyBuffer_Release(&buf);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
508 return NULL;
51969
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
509 }
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
510
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
511 sha1_init(&new->hash_state);
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
512
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
513 if (PyErr_Occurred()) {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
514 Py_DECREF(new);
51969
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
515 if (data_obj)
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
516 PyBuffer_Release(&buf);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
517 return NULL;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
518 }
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 51271
diff changeset
519 if (data_obj) {
48691
ade33e13ec69 Use s* to receive data. Fixes #3552.
Martin v. Löwis <martin@v.loewis.de>
parents: 47581
diff changeset
520 sha1_process(&new->hash_state, buf.buf, buf.len);
51969
e8af4e24f08f Fixed memory leak on failure. This is related to issue5403 but won't crash on py3k.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51574
diff changeset
521 PyBuffer_Release(&buf);
48691
ade33e13ec69 Use s* to receive data. Fixes #3552.
Martin v. Löwis <martin@v.loewis.de>
parents: 47581
diff changeset
522 }
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
523
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
524 return (PyObject *)new;
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
525 }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
526
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
527
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
528 /* List of functions exported by this module */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
529
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
530 static struct PyMethodDef SHA1_functions[] = {
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
531 {"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__},
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
532 {NULL, NULL} /* Sentinel */
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
533 };
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
534
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
535
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
536 /* Initialize this module. */
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
537
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
538 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
539
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
540
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
541 static struct PyModuleDef _sha1module = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
542 PyModuleDef_HEAD_INIT,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
543 "_sha1",
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
544 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
545 -1,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
546 SHA1_functions,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
547 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
548 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
549 NULL,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51969
diff changeset
550 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
551 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
552
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
553 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
554 PyInit__sha1(void)
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
555 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
556 Py_TYPE(&SHA1type) = &PyType_Type;
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
557 if (PyType_Ready(&SHA1type) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
558 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
559 return PyModule_Create(&_sha1module);
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
560 }