Index: portage_checksum.py =================================================================== --- portage_checksum.py (revision 74) +++ portage_checksum.py (revision 87) @@ -22,6 +22,12 @@ #dict of all available hash functions hashfunc_map = {} +# This is only available in Python 2.5 +try: + import hashlib +except ImportError: + hashlib = None + # We _try_ to load this module. If it fails we do the slightly slower fallback. try: import fchksum @@ -32,11 +38,11 @@ except ImportError: import md5 def md5hash(filename): - return pyhash(filename, md5) + return pyhash(filename, md5.new) hashfunc_map["MD5"] = md5hash def sha1hash(filename): - return pyhash(filename, sha) + return pyhash(filename, sha.new) hashfunc_map["SHA1"] = sha1hash # Keep pycrypto optional for now, there are no internal fallbacks for these @@ -44,19 +50,33 @@ import Crypto.Hash.SHA256 def sha256hash(filename): - return pyhash(filename, Crypto.Hash.SHA256) + return pyhash(filename, Crypto.Hash.SHA256.new) hashfunc_map["SHA256"] = sha256hash except ImportError: - pass + if hashlib != None: + def sha256hash(filename): + return pyhash(filename, hashlib.sha256) + hashfunc_map["SHA256"] = sha256hash try: import Crypto.Hash.RIPEMD def rmd160hash(filename): - return pyhash(filename, Crypto.Hash.RIPEMD) + return pyhash(filename, Crypto.Hash.RIPEMD.new) hashfunc_map["RMD160"] = rmd160hash except ImportError: - pass + if hashlib != None: + # Even if hashlib is available, it's not guaranteed + # to support ripemd160, so check first + try: + hashlib.new('ripemd160') + except ValueError: + pass + else: + def rmd160hash(filename): + return pyhash(filename, lambda:hashlib.new('ripemd160')) + hashfunc_map["RMD160"] = rmd160hash + def getsize(filename): size = os.stat(filename).st_size @@ -111,12 +131,12 @@ break return file_is_ok,reason -def pyhash(filename, hashobject): +def pyhash(filename, hashfactory): f = open(filename, 'rb') blocksize = HASHING_BLOCKSIZE data = f.read(blocksize) size = 0L - sum = hashobject.new() + sum = hashfactory() while data: sum.update(data) size = size + len(data)