1818
1919#include "Python.h"
2020#include "structmember.h"
21+ #include "hashlib.h"
2122
2223
2324/* Endianness testing and definitions */
@@ -480,14 +481,17 @@ PyDoc_STRVAR(SHA256_update__doc__,
480481static PyObject *
481482SHA256_update (SHAobject * self , PyObject * args )
482483{
483- unsigned char * cp ;
484- int len ;
484+ PyObject * obj ;
485+ Py_buffer buf ;
485486
486- if (!PyArg_ParseTuple (args , "s# :update" , & cp , & len ))
487+ if (!PyArg_ParseTuple (args , "O :update" , & obj ))
487488 return NULL ;
488489
489- sha_update ( self , cp , len );
490+ GET_BUFFER_VIEW_OR_ERROUT ( obj , & buf );
490491
492+ sha_update (self , buf .buf , buf .len );
493+
494+ PyBuffer_Release (& buf );
491495 Py_INCREF (Py_None );
492496 return Py_None ;
493497}
@@ -614,14 +618,17 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
614618{
615619 static char * kwlist [] = {"string" , NULL };
616620 SHAobject * new ;
617- unsigned char * cp = NULL ;
618- int len ;
621+ PyObject * data_obj = NULL ;
622+ Py_buffer buf ;
619623
620- if (!PyArg_ParseTupleAndKeywords (args , kwdict , "|s# :new" , kwlist ,
621- & cp , & len )) {
624+ if (!PyArg_ParseTupleAndKeywords (args , kwdict , "|O :new" , kwlist ,
625+ & data_obj )) {
622626 return NULL ;
623627 }
624628
629+ if (data_obj )
630+ GET_BUFFER_VIEW_OR_ERROUT (data_obj , & buf );
631+
625632 if ((new = newSHA256object ()) == NULL )
626633 return NULL ;
627634
@@ -631,8 +638,10 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
631638 Py_DECREF (new );
632639 return NULL ;
633640 }
634- if (cp )
635- sha_update (new , cp , len );
641+ if (data_obj ) {
642+ sha_update (new , buf .buf , buf .len );
643+ PyBuffer_Release (& buf );
644+ }
636645
637646 return (PyObject * )new ;
638647}
@@ -645,14 +654,17 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
645654{
646655 static char * kwlist [] = {"string" , NULL };
647656 SHAobject * new ;
648- unsigned char * cp = NULL ;
649- int len ;
657+ PyObject * data_obj = NULL ;
658+ Py_buffer buf ;
650659
651- if (!PyArg_ParseTupleAndKeywords (args , kwdict , "|s# :new" , kwlist ,
652- & cp , & len )) {
660+ if (!PyArg_ParseTupleAndKeywords (args , kwdict , "|O :new" , kwlist ,
661+ & data_obj )) {
653662 return NULL ;
654663 }
655664
665+ if (data_obj )
666+ GET_BUFFER_VIEW_OR_ERROUT (data_obj , & buf );
667+
656668 if ((new = newSHA224object ()) == NULL )
657669 return NULL ;
658670
@@ -662,8 +674,10 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
662674 Py_DECREF (new );
663675 return NULL ;
664676 }
665- if (cp )
666- sha_update (new , cp , len );
677+ if (data_obj ) {
678+ sha_update (new , buf .buf , buf .len );
679+ PyBuffer_Release (& buf );
680+ }
667681
668682 return (PyObject * )new ;
669683}
0 commit comments