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