diff options
| -rw-r--r-- | python/apt_pkgmodule.cc | 50 | ||||
| -rw-r--r-- | tests/test_hashsums.py | 25 |
2 files changed, 73 insertions, 2 deletions
diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc index ca20ef18..e00ce0ca 100644 --- a/python/apt_pkgmodule.cc +++ b/python/apt_pkgmodule.cc @@ -311,7 +311,7 @@ static PyObject *sha1sum(PyObject *Self,PyObject *Args) return 0; } /*}}}*/ -// sha256sum - Compute the sha1sum of a file or string /*{{{*/ +// sha256sum - Compute the sha256sum of a file or string /*{{{*/ // --------------------------------------------------------------------- static const char *doc_sha256sum = "sha256sum(object) -> str\n\n" @@ -356,6 +356,51 @@ static PyObject *sha256sum(PyObject *Self,PyObject *Args) return 0; } /*}}}*/ +// sha512sum - Compute the sha512sum of a file or string /*{{{*/ +// --------------------------------------------------------------------- +static const char *doc_sha512sum = + "sha512sum(object) -> str\n\n" + "Return the sha512sum of the object. 'object' may either be a string, in\n" + "which case the sha512sum of the string is returned, or a file() object\n" + "(or file descriptor), in which case the sha512sum of its contents is\n" + "returned.";; +static PyObject *sha512sum(PyObject *Self,PyObject *Args) +{ + PyObject *Obj; + if (PyArg_ParseTuple(Args,"O",&Obj) == 0) + return 0; + + // Digest of a string. + if (PyBytes_Check(Obj) != 0) + { + char *s; + Py_ssize_t len; + SHA512Summation Sum; + PyBytes_AsStringAndSize(Obj, &s, &len); + Sum.Add((const unsigned char*)s, len); + return CppPyString(Sum.Result().Value()); + } + + // Digest of a file + int Fd = PyObject_AsFileDescriptor(Obj); + if (Fd != -1) + { + SHA512Summation Sum; + struct stat St; + if (fstat(Fd,&St) != 0 || + Sum.AddFD(Fd,St.st_size) == false) + { + PyErr_SetFromErrno(PyExc_SystemError); + return 0; + } + + return CppPyString(Sum.Result().Value()); + } + + PyErr_SetString(PyExc_TypeError,"Only understand strings and files"); + return 0; +} + /*}}}*/ // get_architectures - return the list of architectures /*{{{*/ // --------------------------------------------------------------------- static const char *doc_GetArchitectures = @@ -521,10 +566,11 @@ static PyMethodDef methods[] = {"parse_depends",ParseDepends,METH_VARARGS,doc_ParseDepends}, {"parse_src_depends",ParseSrcDepends,METH_VARARGS,parse_src_depends_doc}, - // Stuff + // Hashes {"md5sum",md5sum,METH_VARARGS,doc_md5sum}, {"sha1sum",sha1sum,METH_VARARGS,doc_sha1sum}, {"sha256sum",sha256sum,METH_VARARGS,doc_sha256sum}, + {"sha512sum",sha512sum,METH_VARARGS,doc_sha512sum}, // multiarch {"get_architectures", GetArchitectures, METH_VARARGS, doc_GetArchitectures}, diff --git a/tests/test_hashsums.py b/tests/test_hashsums.py index 1819e343..bd2dba91 100644 --- a/tests/test_hashsums.py +++ b/tests/test_hashsums.py @@ -66,5 +66,30 @@ class testHashes(unittest.TestCase): with open(self.DATA_WITH_ZERO_PATH) as fobj: self.assertEqual(apt_pkg.sha256sum(fobj), s_hash) + def testSHA512(self): + # simple + s = b"foo" + s_hash = \ + "f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d" \ + "0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19" \ + "594a7eb539453e1ed7" + res = apt_pkg.sha512sum(s) + self.assertEqual(res, s_hash) + # file + with open(self.DATA_PATH) as fobj: + self.assertEqual(apt_pkg.sha512sum(fobj), s_hash) + # with zero (\0) in the string + s = b"foo\0bar" + s_hash = \ + "8c5e791db8f6bfb40eba884f70c9ac52231f01a393e4e55b4576d45" \ + "9a827f34f77e41e7fac806724517b9e96bb42387c5f9bbf325d2f99" \ + "ed52a4aa6abebc3350" + res = apt_pkg.sha512sum(s) + self.assertEqual(res, s_hash) + # file + with open(self.DATA_WITH_ZERO_PATH) as fobj: + self.assertEqual(apt_pkg.sha512sum(fobj), s_hash) + + if __name__ == "__main__": unittest.main() |
