Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Gentoo Portage Developer (http://www.linux-archive.org/gentoo-portage-developer/)
-   -   Manifest2 hash backend provider: mhash (http://www.linux-archive.org/gentoo-portage-developer/581875-manifest2-hash-backend-provider-mhash.html)

"Robin H. Johnson" 09-30-2011 01:27 AM

Manifest2 hash backend provider: mhash
 
Offer mhash as a provider for Manifest2 hash generation and validation.
This is important as none of pycrypto/hashlib/fchksum offer an
accelerated Whirlpool implementaiton. Additionally, the mhash
implementation is accelerated and ships with a rigorious testsuite.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
pym/portage/checksum.py | 19 +++++++++++++++++--
1 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
index b2c9333..c3df36b 100644
--- a/pym/portage/checksum.py
+++ b/pym/portage/checksum.py
@@ -16,7 +16,7 @@ import tempfile
hashfunc_map = {}
hashorigin_map = {}

-def _generate_hash_function(hashtype, hashobject, origin="unknown"):
+def _generate_hash_function(hashtype, hashobject, origin="unknown", hashobjectargs=None):
def pyhash(filename):
"""
Run a checksum against a file.
@@ -41,7 +41,11 @@ def _generate_hash_function(hashtype, hashobject, origin="unknown"):
blocksize = HASHING_BLOCKSIZE
data = f.read(blocksize)
size = 0
- checksum = hashobject()
+ if hashobjectargs is None:
+ checksum = hashobject()
+ else:
+ checksum = hashobject(hashobjectargs)
+
while data:
checksum.update(data)
size = size + len(data)
@@ -103,6 +107,17 @@ try:
except ImportError as e:
pass

+# Try to use mhash if available
+try:
+ import mhash
+ md5hash = _generate_hash_function("MD5", mhash.MHASH, origin="mhash", hashobjectargs=mhash.MHASH_MD5)
+ sha1hash = _generate_hash_function("SHA1", mhash.MHASH, origin="mhash", hashobjectargs=mhash.MHASH_SHA1)
+ sha256hash = _generate_hash_function("SHA256", mhash.MHASH, origin="mhash", hashobjectargs=mhash.MHASH_SHA256)
+ sha512hash = _generate_hash_function("SHA512", mhash.MHASH, origin="mhash", hashobjectargs=mhash.MHASH_SHA512)
+ rmd160hash = _generate_hash_function("RMD160", mhash.MHASH, origin="mhash", hashobjectargs=mhash.MHASH_RIPEMD160)
+ whirlpoolhash = _generate_hash_function("WHIRLPOOL", mhash.MHASH, origin="mhash", hashobjectargs=mhash.MHASH_WHIRLPOOL)
+except ImportError as e:
+ pass

# Use python-fchksum if available, prefer it over all other MD5 implementations
try:
--
1.7.6

"Robin H. Johnson" 09-30-2011 10:51 PM

Manifest2 hash backend provider: mhash
 
On Fri, Sep 30, 2011 at 01:27:41AM +0000, Robin H. Johnson wrote:
> Offer mhash as a provider for Manifest2 hash generation and validation.
> This is important as none of pycrypto/hashlib/fchksum offer an
> accelerated Whirlpool implementaiton. Additionally, the mhash
> implementation is accelerated and ships with a rigorious testsuite.
ferringb has pointed out that mhash holds the GIL lock, and that hashlib
does support whirlpool (despite the docs not mentioning it).

This portion might end up dropped then.

--
Robin Hugh Johnson
Gentoo Linux: Developer, Trustee & Infrastructure Lead
E-Mail : robbat2@gentoo.org
GnuPG FP : 11AC BA4F 4778 E3F6 E4ED F38E B27B 944E 3488 4E85

"Robin H. Johnson" 10-01-2011 07:40 AM

Manifest2 hash backend provider: mhash
 
From: "Robin H. Johnson" <robbat2@gentoo.org>

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 <robbat2@gentoo.org>
---
pym/portage/checksum.py | 19 +++++++++++++++++++
1 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
index 40ae836..c0c7c04 100644
--- a/pym/portage/checksum.py
+++ b/pym/portage/checksum.py
@@ -75,6 +75,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' % s.upper())),
+ origin='mhash')
+except ImportError as e:
+ pass
+
# Use pycrypto when available, prefer it over the internal fallbacks
try:
from Crypto.Hash import SHA256, RIPEMD
--
1.7.7


All times are GMT. The time now is 11:44 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.