computed = hashlib.new(name, data).hexdigest()
self.assertEqual(computed, digest)
- def check_no_unicode(self, algorithm_name):
+ def check_unicode(self, algorithm_name):
# Unicode objects are not allowed as input.
- self.assertRaises(TypeError, getattr(hashlib, algorithm_name), u'spam')
- self.assertRaises(TypeError, hashlib.new, algorithm_name, u'spam')
-
- def test_no_unicode(self):
- self.check_no_unicode('md5')
- self.check_no_unicode('sha1')
- self.check_no_unicode('sha224')
- self.check_no_unicode('sha256')
- self.check_no_unicode('sha384')
- self.check_no_unicode('sha512')
+ expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest()
+ self.assertEqual(getattr(hashlib, algorithm_name)(u'spam').hexdigest(),
+ expected)
+ self.assertEqual(hashlib.new(algorithm_name, u'spam').hexdigest(),
+ expected)
+
+ def test_unicode(self):
+ # In python 2.x unicode is auto-encoded to the system default encoding
+ # when passed to hashlib functions.
+ self.check_unicode('md5')
+ self.check_unicode('sha1')
+ self.check_unicode('sha224')
+ self.check_unicode('sha256')
+ self.check_unicode('sha384')
+ self.check_unicode('sha512')
def test_case_md5_0(self):
self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')
#include "Python.h"
#include "structmember.h"
-#include "hashlib.h"
#ifdef WITH_THREAD
#include "pythread.h"
static PyObject *
EVP_update(EVPobject *self, PyObject *args)
{
- PyObject *obj;
Py_buffer view;
- if (!PyArg_ParseTuple(args, "O:update", &obj))
+ if (!PyArg_ParseTuple(args, "s*:update", &view))
return NULL;
- GET_BUFFER_VIEW_OR_ERROUT(obj, &view, NULL);
-
#ifdef WITH_THREAD
if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
self->lock = PyThread_allocate_lock();
EVP_hash(self, view.buf, view.len);
PyThread_release_lock(self->lock);
Py_END_ALLOW_THREADS
- } else {
- EVP_hash(self, view.buf, view.len);
}
-#else
- EVP_hash(self, view.buf, view.len);
+ else
#endif
+ {
+ EVP_hash(self, view.buf, view.len);
+ }
PyBuffer_Release(&view);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef EVP_methods[] = {
{
static char *kwlist[] = {"name", "string", NULL};
PyObject *name_obj = NULL;
- PyObject *data_obj = NULL;
- Py_buffer view;
+ Py_buffer view = { 0 };
char *nameStr;
const EVP_MD *digest;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
- &name_obj, &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
+ &name_obj, &view)) {
return -1;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, -1);
-
if (!PyArg_Parse(name_obj, "s", &nameStr)) {
PyErr_SetString(PyExc_TypeError, "name must be a string");
- if (data_obj)
- PyBuffer_Release(&view);
+ PyBuffer_Release(&view);
return -1;
}
digest = EVP_get_digestbyname(nameStr);
if (!digest) {
PyErr_SetString(PyExc_ValueError, "unknown hash function");
- if (data_obj)
- PyBuffer_Release(&view);
+ PyBuffer_Release(&view);
return -1;
}
EVP_DigestInit(&self->ctx, digest);
self->name = name_obj;
Py_INCREF(self->name);
- if (data_obj) {
+ if (view.obj) {
if (view.len >= HASHLIB_GIL_MINSIZE) {
Py_BEGIN_ALLOW_THREADS
EVP_hash(self, view.buf, view.len);
{
static char *kwlist[] = {"name", "string", NULL};
PyObject *name_obj = NULL;
- PyObject *data_obj = NULL;
Py_buffer view = { 0 };
PyObject *ret_obj;
char *name;
const EVP_MD *digest;
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
- &name_obj, &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
+ &name_obj, &view)) {
return NULL;
}
return NULL;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
digest = EVP_get_digestbyname(name);
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;
}
static PyObject * \
EVP_new_ ## NAME (PyObject *self, PyObject *args) \
{ \
- PyObject *data_obj = NULL; \
Py_buffer view = { 0 }; \
PyObject *ret_obj; \
\
- if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
+ if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
return NULL; \
} \
\
- if (data_obj) \
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
- \
- ret_obj = EVPnew( \
- CONST_ ## NAME ## _name_obj, \
- NULL, \
- 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); \
+ ret_obj = EVPnew( \
+ CONST_ ## NAME ## _name_obj, \
+ NULL, \
+ CONST_new_ ## NAME ## _ctx_p, \
+ (unsigned char*)view.buf, view.len); \
+ PyBuffer_Release(&view); \
return ret_obj; \
}
#include "Python.h"
#include "structmember.h"
#include "md5.h"
-#include "hashlib.h"
typedef struct {
PyObject_HEAD
static PyObject *
md5_update(md5object *self, PyObject *args)
{
- PyObject *data_obj;
Py_buffer view;
- if (!PyArg_ParseTuple(args, "O:update", &data_obj))
+ if (!PyArg_ParseTuple(args, "s*:update", &view))
return NULL;
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
md5_append(&self->md5, (unsigned char*)view.buf,
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
PyBuffer_Release(&view);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(update_doc,
MD5_new(PyObject *self, PyObject *args)
{
md5object *md5p;
- PyObject *data_obj = NULL;
- Py_buffer view;
+ Py_buffer view = { 0 };
- if (!PyArg_ParseTuple(args, "|O:new", &data_obj))
+ if (!PyArg_ParseTuple(args, "|s*:new", &view))
return NULL;
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
if ((md5p = newmd5object()) == NULL) {
- if (data_obj)
- PyBuffer_Release(&view);
+ PyBuffer_Release(&view);
return NULL;
}
- if (data_obj) {
+ if (view.len > 0) {
md5_append(&md5p->md5, (unsigned char*)view.buf,
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
- PyBuffer_Release(&view);
}
+ PyBuffer_Release(&view);
return (PyObject *)md5p;
}
#include "Python.h"
#include "structmember.h"
-#include "hashlib.h"
/* Endianness testing and definitions */
static PyObject *
SHA256_update(SHAobject *self, PyObject *args)
{
- PyObject *obj;
Py_buffer buf;
- if (!PyArg_ParseTuple(args, "O:update", &obj))
+ if (!PyArg_ParseTuple(args, "s*:update", &buf))
return NULL;
- GET_BUFFER_VIEW_OR_ERROUT(obj, &buf, NULL);
-
sha_update(self, buf.buf, buf.len);
PyBuffer_Release(&buf);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef SHA_methods[] = {
{
static char *kwlist[] = {"string", NULL};
SHAobject *new;
- PyObject *data_obj = NULL;
- Py_buffer buf;
+ Py_buffer buf = { 0 };
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
- &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+ &buf)) {
return NULL;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
if ((new = newSHA256object()) == NULL) {
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
if (PyErr_Occurred()) {
Py_DECREF(new);
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
- if (data_obj) {
+ if (buf.len > 0) {
sha_update(new, buf.buf, buf.len);
- PyBuffer_Release(&buf);
}
+ PyBuffer_Release(&buf);
return (PyObject *)new;
}
{
static char *kwlist[] = {"string", NULL};
SHAobject *new;
- PyObject *data_obj = NULL;
- Py_buffer buf;
+ Py_buffer buf = { 0 };
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
- &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+ &buf)) {
return NULL;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
if ((new = newSHA224object()) == NULL) {
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
if (PyErr_Occurred()) {
Py_DECREF(new);
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
- if (data_obj) {
+ if (buf.len > 0) {
sha_update(new, buf.buf, buf.len);
- PyBuffer_Release(&buf);
}
+ PyBuffer_Release(&buf);
return (PyObject *)new;
}
#include "Python.h"
#include "structmember.h"
-#include "hashlib.h"
#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
static PyObject *
SHA512_update(SHAobject *self, PyObject *args)
{
- PyObject *obj;
Py_buffer buf;
- if (!PyArg_ParseTuple(args, "O:update", &obj))
+ if (!PyArg_ParseTuple(args, "s*:update", &buf))
return NULL;
- GET_BUFFER_VIEW_OR_ERROUT(obj, &buf, NULL);
-
sha512_update(self, buf.buf, buf.len);
PyBuffer_Release(&buf);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef SHA_methods[] = {
{
static char *kwlist[] = {"string", NULL};
SHAobject *new;
- PyObject *data_obj = NULL;
- Py_buffer buf;
+ Py_buffer buf = { 0 };
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
- &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+ &buf)) {
return NULL;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
if ((new = newSHA512object()) == NULL) {
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
if (PyErr_Occurred()) {
Py_DECREF(new);
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
- if (data_obj) {
+ if (buf.len > 0) {
sha512_update(new, buf.buf, buf.len);
- PyBuffer_Release(&buf);
}
+ PyBuffer_Release(&buf);
return (PyObject *)new;
}
{
static char *kwlist[] = {"string", NULL};
SHAobject *new;
- PyObject *data_obj = NULL;
- Py_buffer buf;
+ Py_buffer buf = { 0 };
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
- &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+ &buf)) {
return NULL;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
if ((new = newSHA384object()) == NULL) {
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
if (PyErr_Occurred()) {
Py_DECREF(new);
- if (data_obj)
- PyBuffer_Release(&buf);
+ PyBuffer_Release(&buf);
return NULL;
}
- if (data_obj) {
+ if (buf.len > 0) {
sha512_update(new, buf.buf, buf.len);
- PyBuffer_Release(&buf);
}
+ PyBuffer_Release(&buf);
return (PyObject *)new;
}
#include "Python.h"
#include "structmember.h"
-#include "hashlib.h"
/* Endianness testing and definitions */
static PyObject *
SHA_update(SHAobject *self, PyObject *args)
{
- PyObject *data_obj;
Py_buffer view;
- if (!PyArg_ParseTuple(args, "O:update", &data_obj))
+ if (!PyArg_ParseTuple(args, "s*:update", &view))
return NULL;
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
sha_update(self, (unsigned char*)view.buf,
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
PyBuffer_Release(&view);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef SHA_methods[] = {
{
static char *kwlist[] = {"string", NULL};
SHAobject *new;
- PyObject *data_obj = NULL;
- Py_buffer view;
+ Py_buffer view = { 0 };
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
- &data_obj)) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+ &view)) {
return NULL;
}
- if (data_obj)
- GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
if ((new = newSHAobject()) == NULL) {
- if (data_obj)
- PyBuffer_Release(&view);
+ PyBuffer_Release(&view);
return NULL;
}
if (PyErr_Occurred()) {
Py_DECREF(new);
- if (data_obj)
- PyBuffer_Release(&view);
+ PyBuffer_Release(&view);
return NULL;
}
- if (data_obj) {
+ if (view.len > 0) {
sha_update(new, (unsigned char*)view.buf,
Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
- PyBuffer_Release(&view);
}
+ PyBuffer_Release(&view);
return (PyObject *)new;
}