From e890c121465333d48662be879db441e552281920 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Mar 2010 06:10:16 +0100 Subject: Replaced multihash with pure-python ed2k-hash implementation. --- pyanidb/hash.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pyanidb/hash.py b/pyanidb/hash.py index 4aab45f..9727ca2 100644 --- a/pyanidb/hash.py +++ b/pyanidb/hash.py @@ -1,9 +1,39 @@ -import multihash, threading, time, os +import threading, time, os, hashlib try: import xattr except ImportError: xattr = None +class Hash: + def __init__(self, filename, algorithms): + update_list = [getattr(self, 'update_%s' % a) for a in algorithms] + self.md4_partial = hashlib.new('md4') + self.md4_final = hashlib.new('md4') + self.size_total = 0 + f = open(filename) + data = f.read(131072) + while data: + for u in update_list: + u(data) + data = f.read(131072) + + def update_ed2k(self, data): + pos = 0 + while pos < len(data): + if (not (self.size_total % 9728000)) and self.size_total: + self.md4_final.update(self.md4_partial.digest()) + self.md4_partial = hashlib.new('md4') + size = min(len(data) - pos, 9728000 - (self.size_total % 9728000)) + self.md4_partial.update(data[pos:pos + size]) + pos += size + self.size_total += size + + def ed2k(self): + if self.size_total > 9728000: + self.md4_final.update(self.md4_partial.digest()) + return self.md4_final.hexdigest() + return self.md4_partial.hexdigest() + class File: def __init__(self, name, algorithms, cache): self.name = name @@ -14,7 +44,7 @@ class File: self.read_cache() if False in [hasattr(self, a) for a in algorithms]: self.cached = False - h = multihash.hash_file(name, algorithms) + h = Hash(name, algorithms) for a in algorithms: setattr(self, a, getattr(h, a)()) self.write_cache() -- cgit v1.2.3