mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Issue #3745: Undo the requirement for new buffer API only objects to be passed
to hashlib functions in python 2.x. The module now uses the 's*' for argument parsing which auto encodes unicode objects to the system default encoding for us.
This commit is contained in:
parent
c2fa18ca20
commit
443ec6875f
6 changed files with 86 additions and 159 deletions
|
@ -71,18 +71,23 @@ class HashLibTestCase(unittest.TestCase):
|
||||||
computed = hashlib.new(name, data).hexdigest()
|
computed = hashlib.new(name, data).hexdigest()
|
||||||
self.assertEqual(computed, digest)
|
self.assertEqual(computed, digest)
|
||||||
|
|
||||||
def check_no_unicode(self, algorithm_name):
|
def check_unicode(self, algorithm_name):
|
||||||
# Unicode objects are not allowed as input.
|
# Unicode objects are not allowed as input.
|
||||||
self.assertRaises(TypeError, getattr(hashlib, algorithm_name), u'spam')
|
expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest()
|
||||||
self.assertRaises(TypeError, hashlib.new, algorithm_name, u'spam')
|
self.assertEqual(getattr(hashlib, algorithm_name)(u'spam').hexdigest(),
|
||||||
|
expected)
|
||||||
|
self.assertEqual(hashlib.new(algorithm_name, u'spam').hexdigest(),
|
||||||
|
expected)
|
||||||
|
|
||||||
def test_no_unicode(self):
|
def test_unicode(self):
|
||||||
self.check_no_unicode('md5')
|
# In python 2.x unicode is auto-encoded to the system default encoding
|
||||||
self.check_no_unicode('sha1')
|
# when passed to hashlib functions.
|
||||||
self.check_no_unicode('sha224')
|
self.check_unicode('md5')
|
||||||
self.check_no_unicode('sha256')
|
self.check_unicode('sha1')
|
||||||
self.check_no_unicode('sha384')
|
self.check_unicode('sha224')
|
||||||
self.check_no_unicode('sha512')
|
self.check_unicode('sha256')
|
||||||
|
self.check_unicode('sha384')
|
||||||
|
self.check_unicode('sha512')
|
||||||
|
|
||||||
def test_case_md5_0(self):
|
def test_case_md5_0(self):
|
||||||
self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')
|
self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
#include "hashlib.h"
|
|
||||||
|
|
||||||
#ifdef WITH_THREAD
|
#ifdef WITH_THREAD
|
||||||
#include "pythread.h"
|
#include "pythread.h"
|
||||||
|
@ -218,14 +217,11 @@ PyDoc_STRVAR(EVP_update__doc__,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
EVP_update(EVPobject *self, PyObject *args)
|
EVP_update(EVPobject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *obj;
|
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:update", &obj))
|
if (!PyArg_ParseTuple(args, "s*:update", &view))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(obj, &view, NULL);
|
|
||||||
|
|
||||||
#ifdef WITH_THREAD
|
#ifdef WITH_THREAD
|
||||||
if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
|
if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
|
||||||
self->lock = PyThread_allocate_lock();
|
self->lock = PyThread_allocate_lock();
|
||||||
|
@ -238,17 +234,16 @@ EVP_update(EVPobject *self, PyObject *args)
|
||||||
EVP_hash(self, view.buf, view.len);
|
EVP_hash(self, view.buf, view.len);
|
||||||
PyThread_release_lock(self->lock);
|
PyThread_release_lock(self->lock);
|
||||||
Py_END_ALLOW_THREADS
|
Py_END_ALLOW_THREADS
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
{
|
||||||
EVP_hash(self, view.buf, view.len);
|
EVP_hash(self, view.buf, view.len);
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
EVP_hash(self, view.buf, view.len);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
PyBuffer_Release(&view);
|
PyBuffer_Release(&view);
|
||||||
|
|
||||||
Py_INCREF(Py_None);
|
Py_RETURN_NONE;
|
||||||
return Py_None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef EVP_methods[] = {
|
static PyMethodDef EVP_methods[] = {
|
||||||
|
@ -314,31 +309,25 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"name", "string", NULL};
|
static char *kwlist[] = {"name", "string", NULL};
|
||||||
PyObject *name_obj = NULL;
|
PyObject *name_obj = NULL;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer view = { 0 };
|
||||||
Py_buffer view;
|
|
||||||
char *nameStr;
|
char *nameStr;
|
||||||
const EVP_MD *digest;
|
const EVP_MD *digest;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
|
||||||
&name_obj, &data_obj)) {
|
&name_obj, &view)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, -1);
|
|
||||||
|
|
||||||
if (!PyArg_Parse(name_obj, "s", &nameStr)) {
|
if (!PyArg_Parse(name_obj, "s", &nameStr)) {
|
||||||
PyErr_SetString(PyExc_TypeError, "name must be a string");
|
PyErr_SetString(PyExc_TypeError, "name must be a string");
|
||||||
if (data_obj)
|
PyBuffer_Release(&view);
|
||||||
PyBuffer_Release(&view);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
digest = EVP_get_digestbyname(nameStr);
|
digest = EVP_get_digestbyname(nameStr);
|
||||||
if (!digest) {
|
if (!digest) {
|
||||||
PyErr_SetString(PyExc_ValueError, "unknown hash function");
|
PyErr_SetString(PyExc_ValueError, "unknown hash function");
|
||||||
if (data_obj)
|
PyBuffer_Release(&view);
|
||||||
PyBuffer_Release(&view);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
EVP_DigestInit(&self->ctx, digest);
|
EVP_DigestInit(&self->ctx, digest);
|
||||||
|
@ -346,7 +335,7 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
|
||||||
self->name = name_obj;
|
self->name = name_obj;
|
||||||
Py_INCREF(self->name);
|
Py_INCREF(self->name);
|
||||||
|
|
||||||
if (data_obj) {
|
if (view.obj) {
|
||||||
if (view.len >= HASHLIB_GIL_MINSIZE) {
|
if (view.len >= HASHLIB_GIL_MINSIZE) {
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
EVP_hash(self, view.buf, view.len);
|
EVP_hash(self, view.buf, view.len);
|
||||||
|
@ -471,14 +460,13 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"name", "string", NULL};
|
static char *kwlist[] = {"name", "string", NULL};
|
||||||
PyObject *name_obj = NULL;
|
PyObject *name_obj = NULL;
|
||||||
PyObject *data_obj = NULL;
|
|
||||||
Py_buffer view = { 0 };
|
Py_buffer view = { 0 };
|
||||||
PyObject *ret_obj;
|
PyObject *ret_obj;
|
||||||
char *name;
|
char *name;
|
||||||
const EVP_MD *digest;
|
const EVP_MD *digest;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
|
||||||
&name_obj, &data_obj)) {
|
&name_obj, &view)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -487,16 +475,12 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
|
|
||||||
|
|
||||||
digest = EVP_get_digestbyname(name);
|
digest = EVP_get_digestbyname(name);
|
||||||
|
|
||||||
ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
|
ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
|
||||||
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
view.len);
|
||||||
|
PyBuffer_Release(&view);
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
PyBuffer_Release(&view);
|
|
||||||
return ret_obj;
|
return ret_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,26 +495,19 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
static PyObject * \
|
static PyObject * \
|
||||||
EVP_new_ ## NAME (PyObject *self, PyObject *args) \
|
EVP_new_ ## NAME (PyObject *self, PyObject *args) \
|
||||||
{ \
|
{ \
|
||||||
PyObject *data_obj = NULL; \
|
|
||||||
Py_buffer view = { 0 }; \
|
Py_buffer view = { 0 }; \
|
||||||
PyObject *ret_obj; \
|
PyObject *ret_obj; \
|
||||||
\
|
\
|
||||||
if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
|
if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
if (data_obj) \
|
ret_obj = EVPnew( \
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
|
CONST_ ## NAME ## _name_obj, \
|
||||||
\
|
NULL, \
|
||||||
ret_obj = EVPnew( \
|
CONST_new_ ## NAME ## _ctx_p, \
|
||||||
CONST_ ## NAME ## _name_obj, \
|
(unsigned char*)view.buf, view.len); \
|
||||||
NULL, \
|
PyBuffer_Release(&view); \
|
||||||
CONST_new_ ## NAME ## _ctx_p, \
|
|
||||||
(unsigned char*)view.buf, \
|
|
||||||
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
|
|
||||||
\
|
|
||||||
if (data_obj) \
|
|
||||||
PyBuffer_Release(&view); \
|
|
||||||
return ret_obj; \
|
return ret_obj; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
#include "hashlib.h"
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
|
@ -51,20 +50,16 @@ md5_dealloc(md5object *md5p)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
md5_update(md5object *self, PyObject *args)
|
md5_update(md5object *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *data_obj;
|
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:update", &data_obj))
|
if (!PyArg_ParseTuple(args, "s*:update", &view))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
|
|
||||||
|
|
||||||
md5_append(&self->md5, (unsigned char*)view.buf,
|
md5_append(&self->md5, (unsigned char*)view.buf,
|
||||||
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
||||||
|
|
||||||
PyBuffer_Release(&view);
|
PyBuffer_Release(&view);
|
||||||
Py_INCREF(Py_None);
|
Py_RETURN_NONE;
|
||||||
return Py_None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(update_doc,
|
PyDoc_STRVAR(update_doc,
|
||||||
|
@ -266,26 +261,21 @@ static PyObject *
|
||||||
MD5_new(PyObject *self, PyObject *args)
|
MD5_new(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
md5object *md5p;
|
md5object *md5p;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer view = { 0 };
|
||||||
Py_buffer view;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|O:new", &data_obj))
|
if (!PyArg_ParseTuple(args, "|s*:new", &view))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
|
|
||||||
|
|
||||||
if ((md5p = newmd5object()) == NULL) {
|
if ((md5p = newmd5object()) == NULL) {
|
||||||
if (data_obj)
|
PyBuffer_Release(&view);
|
||||||
PyBuffer_Release(&view);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj) {
|
if (view.len > 0) {
|
||||||
md5_append(&md5p->md5, (unsigned char*)view.buf,
|
md5_append(&md5p->md5, (unsigned char*)view.buf,
|
||||||
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
||||||
PyBuffer_Release(&view);
|
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&view);
|
||||||
|
|
||||||
return (PyObject *)md5p;
|
return (PyObject *)md5p;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
#include "hashlib.h"
|
|
||||||
|
|
||||||
|
|
||||||
/* Endianness testing and definitions */
|
/* Endianness testing and definitions */
|
||||||
|
@ -481,19 +480,15 @@ PyDoc_STRVAR(SHA256_update__doc__,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
SHA256_update(SHAobject *self, PyObject *args)
|
SHA256_update(SHAobject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *obj;
|
|
||||||
Py_buffer buf;
|
Py_buffer buf;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:update", &obj))
|
if (!PyArg_ParseTuple(args, "s*:update", &buf))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf, NULL);
|
|
||||||
|
|
||||||
sha_update(self, buf.buf, buf.len);
|
sha_update(self, buf.buf, buf.len);
|
||||||
|
|
||||||
PyBuffer_Release(&buf);
|
PyBuffer_Release(&buf);
|
||||||
Py_INCREF(Py_None);
|
Py_RETURN_NONE;
|
||||||
return Py_None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef SHA_methods[] = {
|
static PyMethodDef SHA_methods[] = {
|
||||||
|
@ -618,20 +613,15 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"string", NULL};
|
static char *kwlist[] = {"string", NULL};
|
||||||
SHAobject *new;
|
SHAobject *new;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer buf = { 0 };
|
||||||
Py_buffer buf;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
|
||||||
&data_obj)) {
|
&buf)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
|
|
||||||
|
|
||||||
if ((new = newSHA256object()) == NULL) {
|
if ((new = newSHA256object()) == NULL) {
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -639,14 +629,13 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (data_obj) {
|
if (buf.len > 0) {
|
||||||
sha_update(new, buf.buf, buf.len);
|
sha_update(new, buf.buf, buf.len);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&buf);
|
||||||
|
|
||||||
return (PyObject *)new;
|
return (PyObject *)new;
|
||||||
}
|
}
|
||||||
|
@ -659,20 +648,15 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"string", NULL};
|
static char *kwlist[] = {"string", NULL};
|
||||||
SHAobject *new;
|
SHAobject *new;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer buf = { 0 };
|
||||||
Py_buffer buf;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
|
||||||
&data_obj)) {
|
&buf)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
|
|
||||||
|
|
||||||
if ((new = newSHA224object()) == NULL) {
|
if ((new = newSHA224object()) == NULL) {
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -680,14 +664,13 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (data_obj) {
|
if (buf.len > 0) {
|
||||||
sha_update(new, buf.buf, buf.len);
|
sha_update(new, buf.buf, buf.len);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&buf);
|
||||||
|
|
||||||
return (PyObject *)new;
|
return (PyObject *)new;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
#include "hashlib.h"
|
|
||||||
|
|
||||||
#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
|
#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
|
||||||
|
|
||||||
|
@ -547,19 +546,15 @@ PyDoc_STRVAR(SHA512_update__doc__,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
SHA512_update(SHAobject *self, PyObject *args)
|
SHA512_update(SHAobject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *obj;
|
|
||||||
Py_buffer buf;
|
Py_buffer buf;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:update", &obj))
|
if (!PyArg_ParseTuple(args, "s*:update", &buf))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf, NULL);
|
|
||||||
|
|
||||||
sha512_update(self, buf.buf, buf.len);
|
sha512_update(self, buf.buf, buf.len);
|
||||||
|
|
||||||
PyBuffer_Release(&buf);
|
PyBuffer_Release(&buf);
|
||||||
Py_INCREF(Py_None);
|
Py_RETURN_NONE;
|
||||||
return Py_None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef SHA_methods[] = {
|
static PyMethodDef SHA_methods[] = {
|
||||||
|
@ -684,20 +679,15 @@ SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"string", NULL};
|
static char *kwlist[] = {"string", NULL};
|
||||||
SHAobject *new;
|
SHAobject *new;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer buf = { 0 };
|
||||||
Py_buffer buf;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
|
||||||
&data_obj)) {
|
&buf)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
|
|
||||||
|
|
||||||
if ((new = newSHA512object()) == NULL) {
|
if ((new = newSHA512object()) == NULL) {
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -705,14 +695,13 @@ SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (data_obj) {
|
if (buf.len > 0) {
|
||||||
sha512_update(new, buf.buf, buf.len);
|
sha512_update(new, buf.buf, buf.len);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&buf);
|
||||||
|
|
||||||
return (PyObject *)new;
|
return (PyObject *)new;
|
||||||
}
|
}
|
||||||
|
@ -725,20 +714,15 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"string", NULL};
|
static char *kwlist[] = {"string", NULL};
|
||||||
SHAobject *new;
|
SHAobject *new;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer buf = { 0 };
|
||||||
Py_buffer buf;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
|
||||||
&data_obj)) {
|
&buf)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
|
|
||||||
|
|
||||||
if ((new = newSHA384object()) == NULL) {
|
if ((new = newSHA384object()) == NULL) {
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -746,14 +730,13 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (data_obj)
|
PyBuffer_Release(&buf);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (data_obj) {
|
if (buf.len > 0) {
|
||||||
sha512_update(new, buf.buf, buf.len);
|
sha512_update(new, buf.buf, buf.len);
|
||||||
PyBuffer_Release(&buf);
|
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&buf);
|
||||||
|
|
||||||
return (PyObject *)new;
|
return (PyObject *)new;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
|
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "structmember.h"
|
#include "structmember.h"
|
||||||
#include "hashlib.h"
|
|
||||||
|
|
||||||
|
|
||||||
/* Endianness testing and definitions */
|
/* Endianness testing and definitions */
|
||||||
|
@ -429,20 +428,16 @@ PyDoc_STRVAR(SHA_update__doc__,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
SHA_update(SHAobject *self, PyObject *args)
|
SHA_update(SHAobject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *data_obj;
|
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:update", &data_obj))
|
if (!PyArg_ParseTuple(args, "s*:update", &view))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
|
|
||||||
|
|
||||||
sha_update(self, (unsigned char*)view.buf,
|
sha_update(self, (unsigned char*)view.buf,
|
||||||
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
||||||
|
|
||||||
PyBuffer_Release(&view);
|
PyBuffer_Release(&view);
|
||||||
Py_INCREF(Py_None);
|
Py_RETURN_NONE;
|
||||||
return Py_None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef SHA_methods[] = {
|
static PyMethodDef SHA_methods[] = {
|
||||||
|
@ -540,20 +535,15 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
{
|
{
|
||||||
static char *kwlist[] = {"string", NULL};
|
static char *kwlist[] = {"string", NULL};
|
||||||
SHAobject *new;
|
SHAobject *new;
|
||||||
PyObject *data_obj = NULL;
|
Py_buffer view = { 0 };
|
||||||
Py_buffer view;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
|
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
|
||||||
&data_obj)) {
|
&view)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_obj)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
|
|
||||||
|
|
||||||
if ((new = newSHAobject()) == NULL) {
|
if ((new = newSHAobject()) == NULL) {
|
||||||
if (data_obj)
|
PyBuffer_Release(&view);
|
||||||
PyBuffer_Release(&view);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -561,15 +551,14 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (data_obj)
|
PyBuffer_Release(&view);
|
||||||
PyBuffer_Release(&view);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (data_obj) {
|
if (view.len > 0) {
|
||||||
sha_update(new, (unsigned char*)view.buf,
|
sha_update(new, (unsigned char*)view.buf,
|
||||||
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
|
||||||
PyBuffer_Release(&view);
|
|
||||||
}
|
}
|
||||||
|
PyBuffer_Release(&view);
|
||||||
|
|
||||||
return (PyObject *)new;
|
return (PyObject *)new;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue