Mercurial > cpython-lru_cache-weakref
annotate Modules/_hashopenssl.c @ 80272:ec00f8570c55
Merge typo fix.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Mon, 05 Nov 2012 22:23:16 +0200 |
| parents | 11c9a894680e |
| children | 0cc290da01b2 |
| 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 |
|
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 /* 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
|
22 #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
|
23 /* 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
|
24 #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
|
25 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
26 #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
|
27 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
28 #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
|
29 #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
|
30 #endif |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
31 |
|
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
|
32 /* 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
|
33 #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
|
34 #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
|
35 #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
|
36 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
37 typedef struct { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
38 PyObject_HEAD |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
39 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
|
40 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
|
41 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
42 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
|
43 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
44 } EVPobject; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
45 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
46 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
47 static PyTypeObject EVPtype; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
48 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
49 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
50 #define DEFINE_CONSTS_FOR_NEW(Name) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
51 static PyObject *CONST_ ## Name ## _name_obj; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
52 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
53 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
|
54 |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
55 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
|
56 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
|
57 #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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
63 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
64 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
65 static EVPobject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
66 newEVPobject(PyObject *name) |
|
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 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
69 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
70 /* save the name for .name to return */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
71 if (retval != NULL) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
72 Py_INCREF(name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
73 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
|
74 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
75 retval->lock = NULL; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
76 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
77 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
78 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
79 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
80 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
81 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
82 static void |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
83 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
|
84 { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
85 unsigned int process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
86 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
|
87 while (0 < len) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
88 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
|
89 process = MUNCH_SIZE; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
90 else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
91 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
|
92 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
|
93 len -= process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
94 cp += process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
95 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
96 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
97 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
98 /* Internal methods for a hash object */ |
|
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 static void |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
101 EVP_dealloc(EVPobject *self) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
102 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
103 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
104 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
|
105 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
|
106 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
107 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
|
108 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
|
109 PyObject_Del(self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
110 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
111 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
112 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
|
113 { |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
118 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
119 /* External methods for a hash object */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
120 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
121 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
|
122 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
123 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
124 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
|
125 EVP_copy(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
126 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
127 EVPobject *newobj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
128 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
129 if ( (newobj = newEVPobject(self->name))==NULL) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
130 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
131 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
132 locked_EVP_MD_CTX_copy(&newobj->ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
133 return (PyObject *)newobj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
134 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
135 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
136 PyDoc_STRVAR(EVP_digest__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
137 "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
|
138 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
139 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
|
140 EVP_digest(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
141 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
142 unsigned char digest[EVP_MAX_MD_SIZE]; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
143 EVP_MD_CTX temp_ctx; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
144 PyObject *retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
145 unsigned int digest_size; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
146 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
147 locked_EVP_MD_CTX_copy(&temp_ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
148 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
|
149 EVP_DigestFinal(&temp_ctx, digest, NULL); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
150 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44351
diff
changeset
|
151 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
152 EVP_MD_CTX_cleanup(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
153 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
154 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
155 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
156 PyDoc_STRVAR(EVP_hexdigest__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
157 "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
|
158 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
159 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
|
160 EVP_hexdigest(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
161 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
162 unsigned char digest[EVP_MAX_MD_SIZE]; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
163 EVP_MD_CTX temp_ctx; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
164 PyObject *retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
165 char *hex_digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
166 unsigned int i, j, digest_size; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
167 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
168 /* 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
|
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); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
171 EVP_DigestFinal(&temp_ctx, digest, NULL); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
172 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
173 EVP_MD_CTX_cleanup(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
174 |
|
42178
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
175 /* Allocate a new buffer */ |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
176 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
|
177 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
|
178 return PyErr_NoMemory(); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
179 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
180 /* Make hex version of the digest */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
181 for(i=j=0; i<digest_size; i++) { |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
182 unsigned char c; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
183 c = (digest[i] >> 4) & 0xf; |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
184 hex_digest[j++] = Py_hexdigits[c]; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
185 c = (digest[i] & 0xf); |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
186 hex_digest[j++] = Py_hexdigits[c]; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
187 } |
|
42178
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
188 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
|
189 PyMem_Free(hex_digest); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
190 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
191 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
192 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
193 PyDoc_STRVAR(EVP_update__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
194 "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
|
195 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
196 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
197 EVP_update(EVPobject *self, PyObject *args) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
198 { |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
199 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
|
200 Py_buffer view; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
201 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
202 if (!PyArg_ParseTuple(args, "O:update", &obj)) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
203 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
204 |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
205 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
|
206 |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
207 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
208 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
|
209 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
|
210 /* 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
|
211 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
212 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 } else { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
220 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
|
221 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
222 #else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
223 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
|
224 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
225 |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
226 PyBuffer_Release(&view); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
227 Py_RETURN_NONE; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
228 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
229 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
230 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
|
231 {"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
|
232 {"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
|
233 {"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
|
234 {"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
|
235 {NULL, NULL} /* sentinel */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
236 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
237 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
238 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
239 EVP_get_block_size(EVPobject *self, void *closure) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
240 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
241 long block_size; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
242 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
|
243 return PyLong_FromLong(block_size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
244 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
245 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
246 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
247 EVP_get_digest_size(EVPobject *self, void *closure) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
248 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
249 long size; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
250 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
|
251 return PyLong_FromLong(size); |
|
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 PyMemberDef EVP_members[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
255 {"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
|
256 {NULL} /* Sentinel */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
257 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
258 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
259 static PyGetSetDef EVP_getseters[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
260 {"digest_size", |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
261 (getter)EVP_get_digest_size, NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
262 NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
263 NULL}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
264 {"block_size", |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
265 (getter)EVP_get_block_size, NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
266 NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
267 NULL}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
268 {NULL} /* Sentinel */ |
|
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 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
271 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
272 static PyObject * |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
273 EVP_repr(EVPobject *self) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
274 { |
|
59858
9714f8a77b16
Merged revisions 78875 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59688
diff
changeset
|
275 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
|
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 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
279 static int |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
280 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
281 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36232
diff
changeset
|
282 static char *kwlist[] = {"name", "string", NULL}; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
283 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
|
284 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
|
285 Py_buffer view; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
286 char *nameStr; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
287 const EVP_MD *digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
288 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
289 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
|
290 &name_obj, &data_obj)) { |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
291 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
292 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
293 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
294 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
|
295 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
|
296 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
297 if (!PyArg_Parse(name_obj, "s", &nameStr)) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
298 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
|
299 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
300 PyBuffer_Release(&view); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
301 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
302 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
303 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
304 digest = EVP_get_digestbyname(nameStr); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
305 if (!digest) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
306 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
|
307 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
308 PyBuffer_Release(&view); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
309 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
310 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
311 EVP_DigestInit(&self->ctx, digest); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
312 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
313 self->name = name_obj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
314 Py_INCREF(self->name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
315 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
316 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
|
317 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
|
318 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
|
319 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
|
320 Py_END_ALLOW_THREADS |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
321 } else { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
322 EVP_hash(self, view.buf, view.len); |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
323 } |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
324 PyBuffer_Release(&view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
325 } |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
326 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
327 return 0; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
328 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
329 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
330 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
331 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
332 PyDoc_STRVAR(hashtype_doc, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
333 "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
|
334 string of information.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
335 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
336 Methods:\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
337 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
338 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
|
339 digest() -- return the current digest value\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
340 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
|
341 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
|
342 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
343 Attributes:\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
344 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
345 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
|
346 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
|
347 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
348 static PyTypeObject EVPtype = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42178
diff
changeset
|
349 PyVarObject_HEAD_INIT(NULL, 0) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
350 "_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
|
351 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
|
352 0, /*tp_itemsize*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
353 /* methods */ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
354 (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
|
355 0, /*tp_print*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
356 0, /*tp_getattr*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
357 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
|
358 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
|
359 (reprfunc)EVP_repr, /*tp_repr*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
360 0, /*tp_as_number*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
361 0, /*tp_as_sequence*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
362 0, /*tp_as_mapping*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
363 0, /*tp_hash*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
364 0, /*tp_call*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
365 0, /*tp_str*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
366 0, /*tp_getattro*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
367 0, /*tp_setattro*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
368 0, /*tp_as_buffer*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
369 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
370 hashtype_doc, /*tp_doc*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
371 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
|
372 0, /*tp_clear*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
373 0, /*tp_richcompare*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
374 0, /*tp_weaklistoffset*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
375 0, /*tp_iter*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
376 0, /*tp_iternext*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
377 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
|
378 EVP_members, /* tp_members */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
379 EVP_getseters, /* tp_getset */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
380 #if 1 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
381 0, /* tp_base */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
382 0, /* tp_dict */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
383 0, /* tp_descr_get */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
384 0, /* tp_descr_set */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
385 0, /* tp_dictoffset */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
386 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
387 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
388 (initproc)EVP_tp_init, /* tp_init */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
389 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
390 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
391 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
392 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
393 EVPnew(PyObject *name_obj, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
394 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
|
395 const unsigned char *cp, Py_ssize_t len) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
396 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
397 EVPobject *self; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
398 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
399 if (!digest && !initial_ctx) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
400 PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
401 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
402 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
403 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
404 if ((self = newEVPobject(name_obj)) == NULL) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
405 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
406 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
407 if (initial_ctx) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
408 EVP_MD_CTX_copy(&self->ctx, initial_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
409 } else { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
410 EVP_DigestInit(&self->ctx, digest); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
411 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
412 |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
413 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
|
414 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
|
415 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
|
416 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
|
417 Py_END_ALLOW_THREADS |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
418 } else { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
419 EVP_hash(self, cp, len); |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
420 } |
|
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
421 } |
|
35375
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 return (PyObject *)self; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
424 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
425 |
|
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 /* The module-level function: new() */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
428 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
429 PyDoc_STRVAR(EVP_new__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
430 "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
|
431 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
|
432 automatically hashed.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
433 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
434 The MD5 and SHA1 algorithms are always supported.\n"); |
|
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 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
437 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
438 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36232
diff
changeset
|
439 static char *kwlist[] = {"name", "string", NULL}; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
440 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
|
441 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
|
442 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
|
443 PyObject *ret_obj; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
444 char *name; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
445 const EVP_MD *digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
446 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
447 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
|
448 &name_obj, &data_obj)) { |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
449 return NULL; |
|
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 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
452 if (!PyArg_Parse(name_obj, "s", &name)) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
453 PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
454 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
455 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
456 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
457 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
|
458 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
|
459 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
460 digest = EVP_get_digestbyname(name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
461 |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
462 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
|
463 |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
464 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
465 PyBuffer_Release(&view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
466 return ret_obj; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
467 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
468 |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
469 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
470 /* 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
|
471 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
|
472 PyObject *set; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
473 int error; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
474 } _InternalNameMapperState; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
475 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
476 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
477 /* 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
|
478 static void |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
479 _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
|
480 { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
481 _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
|
482 PyObject *py_name; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
483 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
484 assert(state != NULL); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
485 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
|
486 return; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
487 /* 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
|
488 * 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
|
489 * 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
|
490 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
|
491 return; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
492 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
493 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
|
494 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
|
495 state->error = 1; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
496 } else { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
497 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
|
498 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
|
499 state->error = 1; |
|
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 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
502 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
503 |
|
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 /* 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
|
506 static PyObject* |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
507 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
|
508 { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
509 _InternalNameMapperState state; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
510 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
|
511 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
|
512 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
513 state.error = 0; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
514 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
515 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
|
516 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
517 if (state.error) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
518 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
|
519 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
520 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
521 return state.set; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
522 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
523 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
524 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
525 /* |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
526 * This macro generates constructor function definitions for specific |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
527 * hash algorithms. These constructors are much faster than calling |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
528 * 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
|
529 * 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
|
530 * 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
|
531 */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
532 #define GEN_CONSTRUCTOR(NAME) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
533 static PyObject * \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
534 EVP_new_ ## NAME (PyObject *self, PyObject *args) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
535 { \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
536 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
|
537 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
|
538 PyObject *ret_obj; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
539 \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
540 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
541 return NULL; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
542 } \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
543 \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
544 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
|
545 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
|
546 \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
547 ret_obj = EVPnew( \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
548 CONST_ ## NAME ## _name_obj, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
549 NULL, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
550 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
|
551 (unsigned char*)view.buf, \ |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
552 view.len); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
553 \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
554 if (data_obj) \ |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
555 PyBuffer_Release(&view); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
556 return ret_obj; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
557 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
558 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
559 /* a PyMethodDef structure for the constructor */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
560 #define CONSTRUCTOR_METH_DEF(NAME) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
561 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
562 PyDoc_STR("Returns a " #NAME \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
563 " hash object; optionally initialized with a string") \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
564 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
565 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
566 /* used in the init function to setup a constructor */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
567 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \ |
| 42739 | 568 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
569 if (EVP_get_digestbyname(#NAME)) { \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
570 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
571 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
|
572 } \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
573 } while (0); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
574 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
575 GEN_CONSTRUCTOR(md5) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
576 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
|
577 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
578 GEN_CONSTRUCTOR(sha224) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
579 GEN_CONSTRUCTOR(sha256) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
580 GEN_CONSTRUCTOR(sha384) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
581 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
|
582 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
583 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
584 /* List of functions exported by this module */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
585 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
586 static struct PyMethodDef EVP_functions[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
587 {"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
|
588 CONSTRUCTOR_METH_DEF(md5), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
589 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
|
590 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
591 CONSTRUCTOR_METH_DEF(sha224), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
592 CONSTRUCTOR_METH_DEF(sha256), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
593 CONSTRUCTOR_METH_DEF(sha384), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
594 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
|
595 #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
|
596 {NULL, NULL} /* Sentinel */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
597 }; |
|
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 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
600 /* Initialize this module. */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
601 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
602 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
603 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
|
604 PyModuleDef_HEAD_INIT, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
605 "_hashlib", |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
606 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
607 -1, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
608 EVP_functions, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
609 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
610 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
611 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
612 NULL |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
613 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
614 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
615 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
|
616 PyInit__hashlib(void) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
617 { |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
618 PyObject *m, *openssl_md_meth_names; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
619 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
620 OpenSSL_add_all_digests(); |
|
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 /* TODO build EVP_functions openssl_* entries dynamically based |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
623 * on what hashes are supported rather than listing many |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
624 * but having some be unsupported. Only init appropriate |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
625 * constants. */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
626 |
|
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
|
627 Py_TYPE(&EVPtype) = &PyType_Type; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
628 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
|
629 return NULL; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
630 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
631 m = PyModule_Create(&_hashlibmodule); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
632 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
|
633 return NULL; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
634 |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
635 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
|
636 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
|
637 Py_DECREF(m); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
638 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
639 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
640 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
|
641 Py_DECREF(m); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
642 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
643 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
644 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
645 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
646 Py_INCREF(&EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
647 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
648 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
649 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
650 /* these constants are used by the convenience constructors */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
651 INIT_CONSTRUCTOR_CONSTANTS(md5); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
652 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
|
653 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
654 INIT_CONSTRUCTOR_CONSTANTS(sha224); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
655 INIT_CONSTRUCTOR_CONSTANTS(sha256); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
656 INIT_CONSTRUCTOR_CONSTANTS(sha384); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
657 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
|
658 #endif |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
659 return m; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
660 } |
