Gentoo Archives: gentoo-portage-dev

From: "Robin H. Johnson" <robbat2@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [GLEP59v2 4/5] Manifest2 hash backend provider: mhash
Date: Sat, 01 Oct 2011 07:42:28
Message-Id: 1317454855-2794-5-git-send-email-robbat2@gentoo.org
In Reply to: [gentoo-portage-dev] [GLEP59v2 0/5] GLEP59: Manifest2 hash types by "Robin H. Johnson"
1 From: "Robin H. Johnson" <robbat2@g.o>
2
3 Offer mhash as a provider for Manifest2 hash generation and validation.
4 This is important as either of pycrypto or fchksum offer an accelerated
5 Whirlpool implementation, and hashlib might not offer it. Additionally,
6 the mhash implementation is accelerated and ships with a rigorious
7 testsuite.
8
9 Signed-off-by: Robin H. Johnson <robbat2@g.o>
10 ---
11 pym/portage/checksum.py | 19 +++++++++++++++++++
12 1 files changed, 19 insertions(+), 0 deletions(-)
13
14 diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
15 index 40ae836..c0c7c04 100644
16 --- a/pym/portage/checksum.py
17 +++ b/pym/portage/checksum.py
18 @@ -75,6 +75,25 @@ sha1hash = _generate_hash_function("SHA1", _new_sha1, origin="internal")
19 from portage.util.whirlpool import new as _new_whirlpool
20 whirlpoolhash = _generate_hash_function("WHIRLPOOL", _new_whirlpool, origin="bundled")
21
22 +# Try to use mhash if available
23 +# mhash causes GIL presently, so it gets less priority than hashlib and
24 +# pycrypto. However, it might be the only accelerated implementation of
25 +# WHIRLPOOL available.
26 +try:
27 + import mhash, functools
28 + md5hash = _generate_hash_function("MD5", functools.partial(mhash.MHASH, mhash.MHASH_MD5), origin="mhash")
29 + sha1hash = _generate_hash_function("SHA1", functools.partial(mhash.MHASH, mhash.MHASH_SHA1), origin="mhash")
30 + sha256hash = _generate_hash_function("SHA256", functools.partial(mhash.MHASH, mhash.MHASH_SHA256), origin="mhash")
31 + sha512hash = _generate_hash_function("SHA512", functools.partial(mhash.MHASH, mhash.MHASH_SHA512), origin="mhash")
32 + for local_name, hash_name in (("rmd160", "ripemd160"), ("whirlpool", "whirlpool")):
33 + if hasattr(mhash, 'MHASH_%s' % local_name.upper()):
34 + globals()['%shash' % local_name] = \
35 + _generate_hash_function(local_name.upper(), \
36 + functools.partial(mhash.MHASH, getattr(mhash, 'MHASH_%s' % s.upper())), \
37 + origin='mhash')
38 +except ImportError as e:
39 + pass
40 +
41 # Use pycrypto when available, prefer it over the internal fallbacks
42 try:
43 from Crypto.Hash import SHA256, RIPEMD
44 --
45 1.7.7