diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-03-25 06:10:16 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-03-25 06:10:16 +0100 |
commit | e890c121465333d48662be879db441e552281920 (patch) | |
tree | 712834b52cc26cb1de67b63679934b3de9e02812 | |
parent | 0148c3ba5042df191a04dd7443be0d50d27b5498 (diff) |
Replaced multihash with pure-python ed2k-hash implementation.
-rw-r--r-- | pyanidb/hash.py | 34 |
1 files 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() |