Mercurial > cpython-lru_cache-weakref
annotate Modules/_hashopenssl.c @ 70817:dd775b1561ba
Fix markup.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 14 Jun 2011 21:09:55 +0200 |
| parents | 3c9232a684ae |
| children | b6792067aafa d76338eacf7c |
| rev | line source |
|---|---|
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
1 /* Module that wraps all OpenSSL hash algorithms */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
2 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
3 /* |
|
58218
89a9dd8c628d
Merged revisions 77209,77229,77359-77360,77371 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53757
diff
changeset
|
4 * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
5 * Licensed to PSF under a Contributor Agreement. |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
6 * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
7 * Derived from a skeleton of shamodule.c containing work performed by: |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
8 * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
9 * Andrew Kuchling (amk@amk.ca) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
10 * Greg Stein (gstein@lyra.org) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
11 * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
12 */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
13 |
|
36777
1211a68b28dd
Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
14 #define PY_SSIZE_T_CLEAN |
|
1211a68b28dd
Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
15 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
16 #include "Python.h" |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
17 #include "structmember.h" |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
18 #include "hashlib.h" |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
19 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
20 #ifdef WITH_THREAD |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
21 #include "pythread.h" |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
22 #define ENTER_HASHLIB(obj) \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
23 if ((obj)->lock) { \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
24 if (!PyThread_acquire_lock((obj)->lock, 0)) { \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
25 Py_BEGIN_ALLOW_THREADS \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
26 PyThread_acquire_lock((obj)->lock, 1); \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
27 Py_END_ALLOW_THREADS \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
28 } \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
29 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
30 #define LEAVE_HASHLIB(obj) \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
31 if ((obj)->lock) { \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
32 PyThread_release_lock((obj)->lock); \ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
33 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
34 #else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
35 #define ENTER_HASHLIB(obj) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
36 #define LEAVE_HASHLIB(obj) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
37 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
38 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
39 /* EVP is the preferred interface to hashing in OpenSSL */ |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
40 #include <openssl/evp.h> |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
41 /* We use the object interface to discover what hashes OpenSSL supports. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
42 #include <openssl/objects.h> |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
43 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
44 #define MUNCH_SIZE INT_MAX |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
45 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
46 /* TODO(gps): We should probably make this a module or EVPobject attribute |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
47 * to allow the user to optimize based on the platform they're using. */ |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
48 #define HASHLIB_GIL_MINSIZE 2048 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
49 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
50 #ifndef HASH_OBJ_CONSTRUCTOR |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
51 #define HASH_OBJ_CONSTRUCTOR 0 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
52 #endif |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
53 |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
54 /* Minimum OpenSSL version needed to support sha224 and higher. */ |
|
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
55 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000) |
|
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
56 #define _OPENSSL_SUPPORTS_SHA2 |
|
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
57 #endif |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
58 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
59 typedef struct { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
60 PyObject_HEAD |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
61 PyObject *name; /* name of this hash algorithm */ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
62 EVP_MD_CTX ctx; /* OpenSSL message digest context */ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
63 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
64 PyThread_type_lock lock; /* OpenSSL context lock */ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
65 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
66 } EVPobject; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
67 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
68 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
69 static PyTypeObject EVPtype; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
70 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
71 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
72 #define DEFINE_CONSTS_FOR_NEW(Name) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
73 static PyObject *CONST_ ## Name ## _name_obj; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
74 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
75 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
76 |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
77 DEFINE_CONSTS_FOR_NEW(md5) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
78 DEFINE_CONSTS_FOR_NEW(sha1) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
79 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
80 DEFINE_CONSTS_FOR_NEW(sha224) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
81 DEFINE_CONSTS_FOR_NEW(sha256) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
82 DEFINE_CONSTS_FOR_NEW(sha384) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
83 DEFINE_CONSTS_FOR_NEW(sha512) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
84 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
85 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
86 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
87 static EVPobject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
88 newEVPobject(PyObject *name) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
89 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
90 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
91 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
92 /* save the name for .name to return */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
93 if (retval != NULL) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
94 Py_INCREF(name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
95 retval->name = name; |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
96 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
97 retval->lock = NULL; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
98 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
99 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
100 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
101 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
102 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
103 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
104 static void |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
105 EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
106 { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
107 unsigned int process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
108 const unsigned char *cp = (const unsigned char *)vp; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
109 while (0 < len) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
110 if (len > (Py_ssize_t)MUNCH_SIZE) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
111 process = MUNCH_SIZE; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
112 else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
113 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
114 EVP_DigestUpdate(&self->ctx, (const void*)cp, process); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
115 len -= process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
116 cp += process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
117 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
118 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
119 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
120 /* Internal methods for a hash object */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
121 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
122 static void |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
123 EVP_dealloc(EVPobject *self) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
124 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
125 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
126 if (self->lock != NULL) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
127 PyThread_free_lock(self->lock); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
128 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
129 EVP_MD_CTX_cleanup(&self->ctx); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
130 Py_XDECREF(self->name); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
131 PyObject_Del(self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
132 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
133 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
134 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self) |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
135 { |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
136 ENTER_HASHLIB(self); |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
137 EVP_MD_CTX_copy(new_ctx_p, &self->ctx); |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
138 LEAVE_HASHLIB(self); |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
139 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
140 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
141 /* External methods for a hash object */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
142 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
143 PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
144 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
145 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
146 static PyObject * |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
36801
diff
changeset
|
147 EVP_copy(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
148 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
149 EVPobject *newobj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
150 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
151 if ( (newobj = newEVPobject(self->name))==NULL) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
152 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
153 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
154 locked_EVP_MD_CTX_copy(&newobj->ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
155 return (PyObject *)newobj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
156 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
157 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
158 PyDoc_STRVAR(EVP_digest__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
159 "Return the digest value as a string of binary data."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
160 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
161 static PyObject * |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
36801
diff
changeset
|
162 EVP_digest(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
163 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
164 unsigned char digest[EVP_MAX_MD_SIZE]; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
165 EVP_MD_CTX temp_ctx; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
166 PyObject *retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
167 unsigned int digest_size; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
168 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
169 locked_EVP_MD_CTX_copy(&temp_ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
170 digest_size = EVP_MD_CTX_size(&temp_ctx); |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
171 EVP_DigestFinal(&temp_ctx, digest, NULL); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
172 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44351
diff
changeset
|
173 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
174 EVP_MD_CTX_cleanup(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
175 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
176 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
177 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
178 PyDoc_STRVAR(EVP_hexdigest__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
179 "Return the digest value as a string of hexadecimal digits."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
180 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
181 static PyObject * |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
36801
diff
changeset
|
182 EVP_hexdigest(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
183 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
184 unsigned char digest[EVP_MAX_MD_SIZE]; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
185 EVP_MD_CTX temp_ctx; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
186 PyObject *retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
187 char *hex_digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
188 unsigned int i, j, digest_size; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
189 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
190 /* Get the raw (binary) digest value */ |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
191 locked_EVP_MD_CTX_copy(&temp_ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
192 digest_size = EVP_MD_CTX_size(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
193 EVP_DigestFinal(&temp_ctx, digest, NULL); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
194 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
195 EVP_MD_CTX_cleanup(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
196 |
|
42178
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
197 /* Allocate a new buffer */ |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
198 hex_digest = PyMem_Malloc(digest_size * 2 + 1); |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
199 if (!hex_digest) |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
200 return PyErr_NoMemory(); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
201 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
202 /* Make hex version of the digest */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
203 for(i=j=0; i<digest_size; i++) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
204 char c; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
205 c = (digest[i] >> 4) & 0xf; |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
206 c = (c>9) ? c+'a'-10 : c + '0'; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
207 hex_digest[j++] = c; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
208 c = (digest[i] & 0xf); |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
209 c = (c>9) ? c+'a'-10 : c + '0'; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
210 hex_digest[j++] = c; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
211 } |
|
42178
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
212 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2); |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
213 PyMem_Free(hex_digest); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
214 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
215 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
216 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
217 PyDoc_STRVAR(EVP_update__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
218 "Update this hash object's state with the provided string."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
219 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
220 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
221 EVP_update(EVPobject *self, PyObject *args) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
222 { |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
223 PyObject *obj; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
224 Py_buffer view; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
225 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
226 if (!PyArg_ParseTuple(args, "O:update", &obj)) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
227 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
228 |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
229 GET_BUFFER_VIEW_OR_ERROUT(obj, &view); |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
230 |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
231 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
232 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
233 self->lock = PyThread_allocate_lock(); |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
234 /* fail? lock = NULL and we fail over to non-threaded code. */ |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
235 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
236 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
237 if (self->lock != NULL) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
238 Py_BEGIN_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
239 PyThread_acquire_lock(self->lock, 1); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
240 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
241 PyThread_release_lock(self->lock); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
242 Py_END_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
243 } else { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
244 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
245 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
246 #else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
247 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
248 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
249 |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
250 PyBuffer_Release(&view); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
251 Py_RETURN_NONE; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
252 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
253 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
254 static PyMethodDef EVP_methods[] = { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
255 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__}, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
256 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__}, |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
36801
diff
changeset
|
257 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__}, |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
258 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__}, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
259 {NULL, NULL} /* sentinel */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
260 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
261 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
262 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
263 EVP_get_block_size(EVPobject *self, void *closure) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
264 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
265 long block_size; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
266 block_size = EVP_MD_CTX_block_size(&self->ctx); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
267 return PyLong_FromLong(block_size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
268 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
269 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
270 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
271 EVP_get_digest_size(EVPobject *self, void *closure) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
272 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
273 long size; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
274 size = EVP_MD_CTX_size(&self->ctx); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
275 return PyLong_FromLong(size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
276 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
277 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
278 static PyMemberDef EVP_members[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
279 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
280 {NULL} /* Sentinel */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
281 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
282 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
283 static PyGetSetDef EVP_getseters[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
284 {"digest_size", |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
285 (getter)EVP_get_digest_size, NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
286 NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
287 NULL}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
288 {"block_size", |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
289 (getter)EVP_get_block_size, NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
290 NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
291 NULL}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
292 {NULL} /* Sentinel */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
293 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
294 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
295 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
296 static PyObject * |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
297 EVP_repr(EVPobject *self) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
298 { |
|
59858
9714f8a77b16
Merged revisions 78875 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59688
diff
changeset
|
299 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
300 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
301 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
302 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
303 static int |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
304 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
305 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36232
diff
changeset
|
306 static char *kwlist[] = {"name", "string", NULL}; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
307 PyObject *name_obj = NULL; |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
308 PyObject *data_obj = NULL; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
309 Py_buffer view; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
310 char *nameStr; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
311 const EVP_MD *digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
312 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
313 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist, |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
314 &name_obj, &data_obj)) { |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
315 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
316 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
317 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
318 if (data_obj) |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
319 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
320 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
321 if (!PyArg_Parse(name_obj, "s", &nameStr)) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
322 PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
323 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
324 PyBuffer_Release(&view); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
325 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
326 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
327 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
328 digest = EVP_get_digestbyname(nameStr); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
329 if (!digest) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
330 PyErr_SetString(PyExc_ValueError, "unknown hash function"); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
331 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
332 PyBuffer_Release(&view); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
333 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
334 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
335 EVP_DigestInit(&self->ctx, digest); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
336 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
337 self->name = name_obj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
338 Py_INCREF(self->name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
339 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
340 if (data_obj) { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
341 if (view.len >= HASHLIB_GIL_MINSIZE) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
342 Py_BEGIN_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
343 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
344 Py_END_ALLOW_THREADS |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
345 } else { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
346 EVP_hash(self, view.buf, view.len); |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
347 } |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
348 PyBuffer_Release(&view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
349 } |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
350 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
351 return 0; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
352 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
353 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
354 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
355 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
356 PyDoc_STRVAR(hashtype_doc, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
357 "A hash represents the object used to calculate a checksum of a\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
358 string of information.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
359 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
360 Methods:\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
361 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
362 update() -- updates the current digest with an additional string\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
363 digest() -- return the current digest value\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
364 hexdigest() -- return the current digest as a string of hexadecimal digits\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
365 copy() -- return a copy of the current hash object\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
366 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
367 Attributes:\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
368 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
369 name -- the hash algorithm being used by this object\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
370 digest_size -- number of bytes in this hashes output\n"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
371 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
372 static PyTypeObject EVPtype = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42178
diff
changeset
|
373 PyVarObject_HEAD_INIT(NULL, 0) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
374 "_hashlib.HASH", /*tp_name*/ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
375 sizeof(EVPobject), /*tp_basicsize*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
376 0, /*tp_itemsize*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
377 /* methods */ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
378 (destructor)EVP_dealloc, /*tp_dealloc*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
379 0, /*tp_print*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
380 0, /*tp_getattr*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
381 0, /*tp_setattr*/ |
|
51270
debd14c489b9
Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents:
50636
diff
changeset
|
382 0, /*tp_reserved*/ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
383 (reprfunc)EVP_repr, /*tp_repr*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
384 0, /*tp_as_number*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
385 0, /*tp_as_sequence*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
386 0, /*tp_as_mapping*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
387 0, /*tp_hash*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
388 0, /*tp_call*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
389 0, /*tp_str*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
390 0, /*tp_getattro*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
391 0, /*tp_setattro*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
392 0, /*tp_as_buffer*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
393 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
394 hashtype_doc, /*tp_doc*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
395 0, /*tp_traverse*/ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
396 0, /*tp_clear*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
397 0, /*tp_richcompare*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
398 0, /*tp_weaklistoffset*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
399 0, /*tp_iter*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
400 0, /*tp_iternext*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
401 EVP_methods, /* tp_methods */ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
402 EVP_members, /* tp_members */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
403 EVP_getseters, /* tp_getset */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
404 #if 1 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
405 0, /* tp_base */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
406 0, /* tp_dict */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
407 0, /* tp_descr_get */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
408 0, /* tp_descr_set */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
409 0, /* tp_dictoffset */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
410 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
411 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
412 (initproc)EVP_tp_init, /* tp_init */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
413 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
414 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
415 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
416 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
417 EVPnew(PyObject *name_obj, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
418 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
419 const unsigned char *cp, Py_ssize_t len) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
420 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
421 EVPobject *self; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
422 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
423 if (!digest && !initial_ctx) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
424 PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
425 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
426 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
427 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
428 if ((self = newEVPobject(name_obj)) == NULL) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
429 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
430 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
431 if (initial_ctx) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
432 EVP_MD_CTX_copy(&self->ctx, initial_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
433 } else { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
434 EVP_DigestInit(&self->ctx, digest); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
435 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
436 |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
437 if (cp && len) { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
438 if (len >= HASHLIB_GIL_MINSIZE) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
439 Py_BEGIN_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
440 EVP_hash(self, cp, len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
441 Py_END_ALLOW_THREADS |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
442 } else { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
443 EVP_hash(self, cp, len); |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
444 } |
|
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
445 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
446 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
447 return (PyObject *)self; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
448 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
449 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
450 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
451 /* The module-level function: new() */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
452 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
453 PyDoc_STRVAR(EVP_new__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
454 "Return a new hash object using the named algorithm.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
455 An optional string argument may be provided and will be\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
456 automatically hashed.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
457 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
458 The MD5 and SHA1 algorithms are always supported.\n"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
459 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
460 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
461 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
462 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36232
diff
changeset
|
463 static char *kwlist[] = {"name", "string", NULL}; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
464 PyObject *name_obj = NULL; |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
465 PyObject *data_obj = NULL; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
466 Py_buffer view = { 0 }; |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
467 PyObject *ret_obj; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
468 char *name; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
469 const EVP_MD *digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
470 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
471 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist, |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
472 &name_obj, &data_obj)) { |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
473 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
474 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
475 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
476 if (!PyArg_Parse(name_obj, "s", &name)) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
477 PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
478 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
479 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
480 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
481 if (data_obj) |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
482 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
483 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
484 digest = EVP_get_digestbyname(name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
485 |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
486 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
487 |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
488 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
489 PyBuffer_Release(&view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
490 return ret_obj; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
491 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
492 |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
493 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
494 /* State for our callback function so that it can accumulate a result. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
495 typedef struct _internal_name_mapper_state { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
496 PyObject *set; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
497 int error; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
498 } _InternalNameMapperState; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
499 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
500 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
501 /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
502 static void |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
503 _openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
504 { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
505 _InternalNameMapperState *state = (_InternalNameMapperState *)arg; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
506 PyObject *py_name; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
507 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
508 assert(state != NULL); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
509 if (openssl_obj_name == NULL) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
510 return; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
511 /* Ignore aliased names, they pollute the list and OpenSSL appears to |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
512 * have a its own definition of alias as the resulting list still |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
513 * contains duplicate and alternate names for several algorithms. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
514 if (openssl_obj_name->alias) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
515 return; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
516 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
517 py_name = PyUnicode_FromString(openssl_obj_name->name); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
518 if (py_name == NULL) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
519 state->error = 1; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
520 } else { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
521 if (PySet_Add(state->set, py_name) != 0) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
522 Py_DECREF(py_name); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
523 state->error = 1; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
524 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
525 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
526 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
527 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
528 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
529 /* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
530 static PyObject* |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
531 generate_hash_name_list(void) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
532 { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
533 _InternalNameMapperState state; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
534 state.set = PyFrozenSet_New(NULL); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
535 if (state.set == NULL) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
536 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
537 state.error = 0; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
538 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
539 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
540 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
541 if (state.error) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
542 Py_DECREF(state.set); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
543 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
544 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
545 return state.set; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
546 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
547 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
548 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
549 /* |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
550 * This macro generates constructor function definitions for specific |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
551 * hash algorithms. These constructors are much faster than calling |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
552 * the generic one passing it a python string and are noticably |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
553 * faster than calling a python new() wrapper. Thats important for |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
554 * code that wants to make hashes of a bunch of small strings. |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
555 */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
556 #define GEN_CONSTRUCTOR(NAME) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
557 static PyObject * \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
558 EVP_new_ ## NAME (PyObject *self, PyObject *args) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
559 { \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
560 PyObject *data_obj = NULL; \ |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
561 Py_buffer view = { 0 }; \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
562 PyObject *ret_obj; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
563 \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
564 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
565 return NULL; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
566 } \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
567 \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
568 if (data_obj) \ |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
569 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
570 \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
571 ret_obj = EVPnew( \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
572 CONST_ ## NAME ## _name_obj, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
573 NULL, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
574 CONST_new_ ## NAME ## _ctx_p, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
575 (unsigned char*)view.buf, \ |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
576 view.len); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
577 \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
578 if (data_obj) \ |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
579 PyBuffer_Release(&view); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
580 return ret_obj; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
581 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
582 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
583 /* a PyMethodDef structure for the constructor */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
584 #define CONSTRUCTOR_METH_DEF(NAME) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
585 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
586 PyDoc_STR("Returns a " #NAME \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
587 " hash object; optionally initialized with a string") \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
588 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
589 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
590 /* used in the init function to setup a constructor */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
591 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \ |
| 42739 | 592 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
593 if (EVP_get_digestbyname(#NAME)) { \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
594 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
595 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
596 } \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
597 } while (0); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
598 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
599 GEN_CONSTRUCTOR(md5) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
600 GEN_CONSTRUCTOR(sha1) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
601 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
602 GEN_CONSTRUCTOR(sha224) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
603 GEN_CONSTRUCTOR(sha256) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
604 GEN_CONSTRUCTOR(sha384) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
605 GEN_CONSTRUCTOR(sha512) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
606 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
607 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
608 /* List of functions exported by this module */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
609 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
610 static struct PyMethodDef EVP_functions[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
611 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
612 CONSTRUCTOR_METH_DEF(md5), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
613 CONSTRUCTOR_METH_DEF(sha1), |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
614 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
615 CONSTRUCTOR_METH_DEF(sha224), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
616 CONSTRUCTOR_METH_DEF(sha256), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
617 CONSTRUCTOR_METH_DEF(sha384), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
618 CONSTRUCTOR_METH_DEF(sha512), |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
619 #endif |
|
63924
9108e7dcd7d8
Issue #2443: Added a new macro, Py_VA_COPY, which is equivalent to C99
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
59538
diff
changeset
|
620 {NULL, NULL} /* Sentinel */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
621 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
622 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
623 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
624 /* Initialize this module. */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
625 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
626 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
627 static struct PyModuleDef _hashlibmodule = { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
628 PyModuleDef_HEAD_INIT, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
629 "_hashlib", |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
630 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
631 -1, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
632 EVP_functions, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
633 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
634 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
635 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
636 NULL |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
637 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
638 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
639 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
|
640 PyInit__hashlib(void) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
641 { |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
642 PyObject *m, *openssl_md_meth_names; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
643 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
644 OpenSSL_add_all_digests(); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
645 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
646 /* TODO build EVP_functions openssl_* entries dynamically based |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
647 * on what hashes are supported rather than listing many |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
648 * but having some be unsupported. Only init appropriate |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
649 * constants. */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
650 |
|
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
|
651 Py_TYPE(&EVPtype) = &PyType_Type; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
652 if (PyType_Ready(&EVPtype) < 0) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
653 return NULL; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
654 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
655 m = PyModule_Create(&_hashlibmodule); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
656 if (m == NULL) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
657 return NULL; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
658 |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
659 openssl_md_meth_names = generate_hash_name_list(); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
660 if (openssl_md_meth_names == NULL) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
661 Py_DECREF(m); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
662 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
663 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
664 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
665 Py_DECREF(m); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
666 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
667 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
668 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
669 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
670 Py_INCREF(&EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
671 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
672 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
673 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
674 /* these constants are used by the convenience constructors */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
675 INIT_CONSTRUCTOR_CONSTANTS(md5); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
676 INIT_CONSTRUCTOR_CONSTANTS(sha1); |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
677 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
678 INIT_CONSTRUCTOR_CONSTANTS(sha224); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
679 INIT_CONSTRUCTOR_CONSTANTS(sha256); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
680 INIT_CONSTRUCTOR_CONSTANTS(sha384); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
681 INIT_CONSTRUCTOR_CONSTANTS(sha512); |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
682 #endif |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
683 return m; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
684 } |
