##// END OF EJS Templates
sha1dc: use buffer protocol when parsing arguments...
Gregory Szorc -
r44546:dc9b5348 default
parent child Browse files
Show More
@@ -25,8 +25,8 b' typedef struct {'
25
25
26 static int pysha1ctx_init(pysha1ctx *self, PyObject *args)
26 static int pysha1ctx_init(pysha1ctx *self, PyObject *args)
27 {
27 {
28 const char *data = NULL;
28 Py_buffer data;
29 Py_ssize_t len;
29 data.obj = NULL;
30
30
31 SHA1DCInit(&(self->ctx));
31 SHA1DCInit(&(self->ctx));
32 /* We don't want "safe" sha1s, wherein sha1dc can give you a
32 /* We don't want "safe" sha1s, wherein sha1dc can give you a
@@ -34,11 +34,19 b' static int pysha1ctx_init(pysha1ctx *sel'
34 collision. We just want to detect collisions.
34 collision. We just want to detect collisions.
35 */
35 */
36 SHA1DCSetSafeHash(&(self->ctx), 0);
36 SHA1DCSetSafeHash(&(self->ctx), 0);
37 if (!PyArg_ParseTuple(args, PY23("|s#", "|y#"), &data, &len)) {
37 if (!PyArg_ParseTuple(args, PY23("|s*", "|y*"), &data)) {
38 return -1;
38 return -1;
39 }
39 }
40 if (data) {
40 if (data.obj) {
41 SHA1DCUpdate(&(self->ctx), data, len);
41 if (!PyBuffer_IsContiguous(&data, 'C') || data.ndim > 1) {
42 PyErr_SetString(PyExc_BufferError,
43 "buffer must be contiguous and single dimension");
44 PyBuffer_Release(&data);
45 return -1;
46 }
47
48 SHA1DCUpdate(&(self->ctx), data.buf, data.len);
49 PyBuffer_Release(&data);
42 }
50 }
43 return 0;
51 return 0;
44 }
52 }
@@ -50,12 +58,18 b' static void pysha1ctx_dealloc(pysha1ctx '
50
58
51 static PyObject *pysha1ctx_update(pysha1ctx *self, PyObject *args)
59 static PyObject *pysha1ctx_update(pysha1ctx *self, PyObject *args)
52 {
60 {
53 const char *data;
61 Py_buffer data;
54 Py_ssize_t len;
62 if (!PyArg_ParseTuple(args, PY23("s*", "y*"), &data)) {
55 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &data, &len)) {
56 return NULL;
63 return NULL;
57 }
64 }
58 SHA1DCUpdate(&(self->ctx), data, len);
65 if (!PyBuffer_IsContiguous(&data, 'C') || data.ndim > 1) {
66 PyErr_SetString(PyExc_BufferError,
67 "buffer must be contiguous and single dimension");
68 PyBuffer_Release(&data);
69 return NULL;
70 }
71 SHA1DCUpdate(&(self->ctx), data.buf, data.len);
72 PyBuffer_Release(&data);
59 Py_RETURN_NONE;
73 Py_RETURN_NONE;
60 }
74 }
61
75
@@ -45,6 +45,26 b' class hashertestsbase(object):'
45 h.digest(),
45 h.digest(),
46 )
46 )
47
47
48 def test_bytes_like_types(self):
49 h = self.hasher()
50 h.update(bytearray(b'foo'))
51 h.update(memoryview(b'baz'))
52 self.assertEqual(
53 '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
54 )
55
56 h = self.hasher(bytearray(b'foo'))
57 h.update(b'baz')
58 self.assertEqual(
59 '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
60 )
61
62 h = self.hasher(memoryview(b'foo'))
63 h.update(b'baz')
64 self.assertEqual(
65 '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
66 )
67
48
68
49 class hashlibtests(unittest.TestCase, hashertestsbase):
69 class hashlibtests(unittest.TestCase, hashertestsbase):
50 hasher = hashlib.sha1
70 hasher = hashlib.sha1
General Comments 0
You need to be logged in to leave comments. Login now