public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/
Date: Wed, 19 Jun 2013 18:59:05 +0000 (UTC)	[thread overview]
Message-ID: <1371667413.ce623a4736308104f0c11e27c2e94feeda7bbb26.dywi@gentoo> (raw)
Message-ID: <20130619185905.CgkFcdTsbUTycHB5S1kYLln_MRwGNlP0YmFg8QoxlV0@z> (raw)

commit:     ce623a4736308104f0c11e27c2e94feeda7bbb26
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Wed Jun 19 18:43:33 2013 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> 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 ):


             reply	other threads:[~2013-06-19 18:59 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-19 18:58 André Erdmann [this message]
2013-06-19 18:59 ` [gentoo-commits] proj/R_overlay:master commit in: roverlay/ André Erdmann
  -- strict thread matches above, loose matches on Subject: below --
2013-07-23  7:51 André Erdmann
2013-07-23  7:51 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-23  7:51 [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-07-19 18:00 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-19 18:00 André Erdmann
2013-07-17 18:05 André Erdmann
2013-07-15 22:31 André Erdmann
2013-07-12 13:57 [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-07-12 13:57 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-08 22:47 André Erdmann
2013-07-08 22:47 André Erdmann
2013-07-05 16:55 André Erdmann
2013-07-05 16:55 André Erdmann
2013-07-05 16:55 André Erdmann
2013-06-22 15:24 [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-22 15:14 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-22 15:24 [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-22 15:14 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-22 15:24 [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-20 23:40 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-22 15:24 [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-20 23:40 ` [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-19 18:58 André Erdmann
2013-06-05 18:08 André Erdmann
2013-06-05 18:08 André Erdmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1371667413.ce623a4736308104f0c11e27c2e94feeda7bbb26.dywi@gentoo \
    --to=dywi@mailerd.de \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox