summaryrefslogtreecommitdiff
path: root/multihash/__init__.py
blob: d31920ff3c4aac65739b4d7e9a1be65c61f4f026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from _multihash import CRC32, Ed2k, MD5, SHA1, _hash_file

hashes = {
	'crc32': CRC32,
	'ed2k': Ed2k,
	'md5': MD5,
	'sha1': SHA1}

class Multihash:
	def __init__(self, *args):
		if not args:
			args = hashes.keys()
		h = None
		for hash in args:
			h = hashes[hash](h)
			setattr(self, hash, h.digest)
		self.update = h.update
		self.first = h

def hash_file(name, *algorithms):
	f = open(name)
	h = Multihash(*algorithms)
	_hash_file(f.fileno(), h.first)
	return h