From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id B3B4E1381F3 for ; Wed, 19 Jun 2013 18:59:08 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2D9BBE077F; Wed, 19 Jun 2013 18:59:08 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 80FFDE077F for ; Wed, 19 Jun 2013 18:59:07 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 5FB2F33E61B for ; Wed, 19 Jun 2013 18:59:06 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 0E862E545C for ; Wed, 19 Jun 2013 18:59:05 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1371667413.ce623a4736308104f0c11e27c2e94feeda7bbb26.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/digest.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: ce623a4736308104f0c11e27c2e94feeda7bbb26 X-VCS-Branch: master Date: Wed, 19 Jun 2013 18:59:05 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 47a54fb2-2706-4e5b-96bd-9a7375a118ef X-Archives-Hash: 5d5c57b9c29d1134c6b59744a6c2b313 Message-ID: <20130619185905.CgkFcdTsbUTycHB5S1kYLln_MRwGNlP0YmFg8QoxlV0@z> commit: ce623a4736308104f0c11e27c2e94feeda7bbb26 Author: André Erdmann mailerd de> AuthorDate: Wed Jun 19 18:43:33 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Wed Jun 19 18:43:33 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=ce623a47 roverlay/digest: support sha* and multihash mode This commit adds support for sha1/sha256/sha512 (via hashlib) and whirlpool (via portage.util). It also adds the mulihash[_file]() function(s) that create more than one digest at once. --- roverlay/digest.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/roverlay/digest.py b/roverlay/digest.py index 218f98c..983ca1a 100644 --- a/roverlay/digest.py +++ b/roverlay/digest.py @@ -11,23 +11,98 @@ __all__ = [ 'digest_compare', 'digest_supported', ] import hashlib +import portage.util.whirlpool -def md5sum_file ( fh, binary_digest=False ): - """Returns the md5 sum for an already opened file.""" - md5 = hashlib.md5() - blocksize = 16384 +_HASH_CREATE_MAP = { + 'md5' : hashlib.md5, + 'sha1' : hashlib.sha1, + 'sha256' : hashlib.sha256, + 'sha512' : hashlib.sha512, + 'whirlpool' : portage.util.whirlpool.new, +} +def _generic_obj_hash ( hashobj, fh, binary_digest=False, blocksize=16384 ): block = fh.read ( blocksize ) while block: - md5.update ( block ) + hashobj.update ( block ) block = fh.read ( blocksize ) - return md5.digest() if binary_digest else md5.hexdigest() + return hashobj.digest() if binary_digest else hashobj.hexdigest() +# --- end of _hashsum_generic (...) --- + +def multihash ( fh, hashlist, binary_digest=False, blocksize=16384 ): + """Calculates multiple digests for an already openened file and returns the + resulting hashes as dict. + + arguments: + * fh -- file handle + * hashlist -- iterable with hash names (e.g. md5) + * binary_digest -- whether the hashes should be binary or not + * blocksize -- block size for reading + """ + hashobj_dict = { + h: _HASH_CREATE_MAP[h]() for h in hashlist + } + block = fh.read ( blocksize ) + while block: + for hashobj in hashobj_dict.values(): + hashobj.update ( block ) + block = fh.read ( blocksize ) + + if binary_digest: + return { h: hashobj.digest() for h, hashobj in hashobj_dict.items() } + else: + return { h: hashobj.hexdigest() for h, hashobj in hashobj_dict.items() } +# --- end of multihash (...) --- + +def multihash_file ( filepath, digest_types, **kwargs ): + """Calculates multiple digests for the given file path. + + Returns an empty dict if digest_types is empty. + + arguments: + * filepath -- + * digest_types -- + * **kwargs -- passed to multihash() + """ + if digest_types: + with open ( filepath, mode='rb' ) as fh: + hashdict = multihash ( fh, digest_types, **kwargs ) + return hashdict + else: + return dict() +# --- end of multihash_file (...) --- + +def md5sum_file ( fh, binary_digest=False ): + """Returns the md5 sum for an already opened file.""" + return _generic_obj_hash ( hashlib.md5(), fh, binary_digest ) # --- end of md5sum_file (...) --- +def sha1_file ( fh, binary_digest=False ): + return _generic_obj_hash ( hashlib.sha1(), fh, binary_digest ) +# --- end of sha1_file (...) --- + +def sha256_file ( fh, binary_digest=False ): + return _generic_obj_hash ( hashlib.sha256(), fh, binary_digest ) +# --- end of sha256_file (...) --- + +def sha512_file ( fh, binary_digest=False ): + return _generic_obj_hash ( hashlib.sha512(), fh, binary_digest ) +# --- end of sha512_file (...) --- + +def whirlpool_file ( fh, binary_digest=False ): + return _generic_obj_hash ( + portage.util.whirlpool.new(), fh, binary_digest + ) +# --- end of whirlpool_file (...) --- +# TODO: remove _DIGEST_MAP = dict ( - md5 = md5sum_file, + md5 = md5sum_file, + sha1 = sha1_file, + sha256 = sha256_file, + sha512 = sha512_file, + whirlpool = whirlpool_file, ) def digest_supported ( digest_type ):