From f93b71da9bcd01d7c1db797d9a84b95cbdabde0b Mon Sep 17 00:00:00 2001 From: zyp Date: Thu, 19 Oct 2006 15:11:00 +0000 Subject: [project @ zyp-20061019151100-032626b43d2f238b] [project @ 59] Added hash_file-function. --- multihash/__init__.py | 9 ++++++++- src/hash.cpp | 8 ++++++++ src/hash.h | 4 +++- src/wrapper.cpp | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/multihash/__init__.py b/multihash/__init__.py index 5e30186..9be422f 100644 --- a/multihash/__init__.py +++ b/multihash/__init__.py @@ -1,4 +1,4 @@ -from _multihash import CRC32, Ed2k, MD5, SHA1 +from _multihash import CRC32, Ed2k, MD5, SHA1, _hash_file hashes = { 'crc32': CRC32, @@ -15,3 +15,10 @@ class Multihash: h = hashes[hash](h) setattr(self, hash, h.digest) self.update = h.update + self.first = h + +def hash_file(name, *args): + f = open(name) + h = Multihash() + _hash_file(f.fileno(), h.first) + return h \ No newline at end of file diff --git a/src/hash.cpp b/src/hash.cpp index 89b14cc..82df4c0 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -60,4 +60,12 @@ namespace Multihash { std::string Hash::hash_digest() { throw std::runtime_error("Not implemented."); } + + void _hash_file(int fileno, Hash* hash) { + char buf[32768]; + int s; + while((s = read(fileno, buf, 32768)) > 0) { + hash->_update(buf, s); + } + } } diff --git a/src/hash.h b/src/hash.h index d2771a5..571ae02 100644 --- a/src/hash.h +++ b/src/hash.h @@ -13,15 +13,17 @@ namespace Multihash { private: std::string digest_str; Hash* next; - void _update(const char* data, int length); protected: virtual void hash_update(const char* data, int length); virtual std::string hash_digest(); public: Hash(Hash* n = 0); void update(std::string data); + void _update(const char* data, int length); std::string digest(); }; + + void _hash_file(int fileno, Hash* hash); } #endif // _HASH_H_ diff --git a/src/wrapper.cpp b/src/wrapper.cpp index cbe86f5..a93dae2 100644 --- a/src/wrapper.cpp +++ b/src/wrapper.cpp @@ -26,4 +26,6 @@ BOOST_PYTHON_MODULE(_multihash) class_ >("SHA1") .def(init >()); + + def("_hash_file", Multihash::_hash_file); } -- cgit v1.2.3