import itertools import string class HashBlacklist(object): """Turn a hashlib-like object into a hash that returns None for some blacklisted hashes instead of the real hash value. We only work with hexdigests here, so diget() disappears. The methods copy and update as well as the name attribute keep working as expected. """ def __init__(self, hashobj, blacklist=()): """ @param hashobj: a hashlib-like object @param blacklist: an object providing __contains__. hexdigest values which are contained in the blacklist are turned into None values """ self.hashobj = hashobj self.blacklist = blacklist self.update = self.hashobj.update @property def name(self): return self.hashobj.name def hexdigest(self): digest = self.hashobj.hexdigest() if digest in self.blacklist: return None return digest def copy(self): return HashBlacklist(self.hashobj.copy(), self.blacklist) class HashBlacklistContent(object): """Turn a hashlib-like object into a hash that returns None for some blacklisted content instead of the real hash value. Unlike HashBlacklist, not the output of the hash is considered, but its input.""" def __init__(self, hashobj, blacklist=(), maxlen=None): """ @param hashobj: a hashlib-like object @param blacklist: an object providing __contains__. hash inputs which are contained in the blacklist are turned into None values @param maxlen: the maximum length of a blacklisted input. Defaults to max(map(len, blacklist)), so if it is absent, the blacklist must support iteration. """ self.hashobj = hashobj self.blacklist = blacklist if maxlen is None: # the chain avoids passing the empty sequence to max maxlen = max(itertools.chain((0,), itertools.imap(len, blacklist))) self.maxlen = maxlen self.stored = "" @property def name(self): return self.hashobj.name def update(self, data): if self.stored is not None: self.stored += data if len(self.stored) > self.maxlen: self.stored = None self.hashobj.update(data) def digest(self): if self.stored is not None and self.stored in self.blacklist: return None return self.hashobj.digest() def hexdigest(self): if self.stored is not None and self.stored in self.blacklist: return None return self.hashobj.hexdigest() def copy(self): return HashBlacklistContent(self.hashobj.copy(), self.blacklist, self.maxlen) class DecompressedHash(object): """Apply a decompression function before the hash. This class provides the hashlib interface (update, hexdigest, copy) excluding digest and name.""" def __init__(self, decompressor, hashobj): """ @param decompressor: a decompression object like bz2.BZ2Decompressor or lzma.LZMADecompressor. It has to provide methods decompress and copy as well as an unused_data attribute. It may provide a flush method. @param hashobj: a hashlib-like obj providing methods update, hexdigest and copy """ self.decompressor = decompressor self.hashobj = hashobj def update(self, data): self.hashobj.update(self.decompressor.decompress(data)) def hexdigest(self): if not hasattr(self.decompressor, "flush"): if self.decompressor.unused_data: raise ValueError("decompressor did not consume all data") return self.hashobj.hexdigest() tmpdecomp = self.decompressor.copy() data = tmpdecomp.flush() if tmpdecomp.unused_data: raise ValueError("decompressor did not consume all data") tmphash = self.hashobj.copy() tmphash.update(data) return tmphash.hexdigest() def copy(self): return DecompressedHash(self.decompressor.copy(), self.hashobj.copy()) class SuppressingHash(object): """A hash that silences exceptions from the update and hexdigest methods of a hashlib-like object. If an exception has occured, hexdigest always returns None.""" def __init__(self, hashobj, exceptions=()): """ @param hashobj: a hashlib-like object providing methods update, copy and hexdigest. If a name attribute is present, it is mirrored as well. @type exceptions: tuple @param exceptions: exception classes to be suppressed """ self.hashobj = hashobj self.exceptions = exceptions if hasattr(hashobj, "name"): self.name = hashobj.name def update(self, data): if self.hashobj: try: self.hashobj.update(data) except self.exceptions: self.hashobj = None def hexdigest(self): if self.hashobj: try: return self.hashobj.hexdigest() except self.exceptions: self.hashobj = None return None def copy(self): if self.hashobj: return SuppressingHash(self.hashobj.copy(), self.exceptions) return SuppressingHash(None, self.exceptions) def hash_file(hashobj, filelike, blocksize=65536): """Feed the entire contents from the given filelike to the given hashobj. @param hashobj: hashlib-like object providing an update method @param filelike: file-like object providing read(size) """ data = filelike.read(blocksize) while data: hashobj.update(data) data = filelike.read(blocksize) return hashobj class HashedStream(object): """A file-like object, that supports sequential reading and hashes the contents on the fly.""" def __init__(self, filelike, hashobj): """ @param filelike: a file-like object, that must support the read method @param hashobj: a hashlib-like object providing update and hexdigest """ self.filelike = filelike self.hashobj = hashobj def read(self, length): data = self.filelike.read(length) self.hashobj.update(data) return data def hexdigest(self): return self.hashobj.hexdigest() class RateBinary(object): """Compute the ratio of printable characters the total number of characters. Not a hash really.""" printable = set(string.printable) name = "ratebinary" def __init__(self): self.length = 0 self.good = 0 def update(self, data): self.length += len(data) printable = self.printable self.good += len([c for c in data if c in printable]) def hexdigest(self): if not self.length: return "NaN" return "%0.3f" % (float(self.good) / float(self.length)) def copy(self): new = RateBinary() new.length = self.length new.good = self.good