From faf87ba9877e3b5a7866c6649f956f15950e789a Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sat, 1 Oct 2011 07:40:54 +0000 Subject: [PATCH] Manifest2 hash backend provider: mhash Offer mhash as a provider for Manifest2 hash generation and validation. This is important as either of pycrypto or fchksum offer an accelerated Whirlpool implementation, and hashlib might not offer it. Additionally, the mhash implementation is accelerated and ships with a rigorious testsuite. Signed-off-by: Robin H. Johnson --- pym/portage/checksum.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py index 6bace4d6a..a4a744ba9 100644 --- a/pym/portage/checksum.py +++ b/pym/portage/checksum.py @@ -80,6 +80,25 @@ sha1hash = _generate_hash_function("SHA1", _new_sha1, origin="internal") from portage.util.whirlpool import new as _new_whirlpool whirlpoolhash = _generate_hash_function("WHIRLPOOL", _new_whirlpool, origin="bundled") +# Try to use mhash if available +# mhash causes GIL presently, so it gets less priority than hashlib and +# pycrypto. However, it might be the only accelerated implementation of +# WHIRLPOOL available. +try: + import mhash, functools + md5hash = _generate_hash_function("MD5", functools.partial(mhash.MHASH, mhash.MHASH_MD5), origin="mhash") + sha1hash = _generate_hash_function("SHA1", functools.partial(mhash.MHASH, mhash.MHASH_SHA1), origin="mhash") + sha256hash = _generate_hash_function("SHA256", functools.partial(mhash.MHASH, mhash.MHASH_SHA256), origin="mhash") + sha512hash = _generate_hash_function("SHA512", functools.partial(mhash.MHASH, mhash.MHASH_SHA512), origin="mhash") + for local_name, hash_name in (("rmd160", "ripemd160"), ("whirlpool", "whirlpool")): + if hasattr(mhash, 'MHASH_%s' % local_name.upper()): + globals()['%shash' % local_name] = \ + _generate_hash_function(local_name.upper(), \ + functools.partial(mhash.MHASH, getattr(mhash, 'MHASH_%s' % hash_name.upper())), \ + origin='mhash') +except ImportError: + pass + # Use pycrypto when available, prefer it over the internal fallbacks try: from Crypto.Hash import SHA256, RIPEMD -- 2.26.2