From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id C369B139694 for ; Mon, 13 Mar 2017 21:46:54 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3154121C04E; Mon, 13 Mar 2017 21:46:49 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 10E0021C072 for ; Mon, 13 Mar 2017 21:46:49 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 4699D341134 for ; Mon, 13 Mar 2017 21:46:48 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 864196738 for ; Mon, 13 Mar 2017 21:46:45 +0000 (UTC) From: "Michał Górny" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" Message-ID: <1489441589.74ac4204f1d17c07f1b84c139b2484da8b95b98d.mgorny@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/ X-VCS-Repository: proj/portage X-VCS-Files: pym/portage/checksum.py X-VCS-Directories: pym/portage/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: 74ac4204f1d17c07f1b84c139b2484da8b95b98d X-VCS-Branch: master Date: Mon, 13 Mar 2017 21:46:45 +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: fa7d5d44-703f-46c3-96aa-d915051c364e X-Archives-Hash: f6e6cc41bd5b785f0710d6678ac6ce14 commit: 74ac4204f1d17c07f1b84c139b2484da8b95b98d Author: Michał Górny gentoo org> AuthorDate: Sun Mar 12 14:16:23 2017 +0000 Commit: Michał Górny gentoo org> CommitDate: Mon Mar 13 21:46:29 2017 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=74ac4204 portage.checksum: Support getting byte string checksums Add a checksum_str() method to Portage hashes and a matching function to make it possible to compute checksums of arbitrary bytestrings rather than just files. pym/portage/checksum.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py index 67d6a544f..9ba251f29 100644 --- a/pym/portage/checksum.py +++ b/pym/portage/checksum.py @@ -59,6 +59,18 @@ class _generate_hash_function(object): hashfunc_map[hashtype] = self hashorigin_map[hashtype] = origin + def checksum_str(self, data): + """ + Obtain a checksum of a byte-string. + + @param data: Data to hash + @type data: bytes + @return: The hash of the data (hex-digest) + """ + checksum = self._hashobject() + checksum.update(data) + return checksum.hexdigest() + def checksum_file(self, filename): """ Run a checksum against a file. @@ -461,3 +473,20 @@ def perform_multiple_checksums(filename, hashes=["MD5"], calc_prelink=0): raise portage.exception.DigestException(x+" hash function not available (needs dev-python/pycrypto or >=dev-lang/python-2.5)") rVal[x] = perform_checksum(filename, x, calc_prelink)[0] return rVal + + +def checksum_str(data, hashname="MD5"): + """ + Run a specific checksum against a byte string. + + @param filename: Data to checksum + @type filename: Bytes + @param hashname: The type of hash function to run + @type hashname: String + @rtype: String + @return: The hash (hex-digest) of the data + """ + if hashname not in hashfunc_map: + raise portage.exception.DigestException(hashname + \ + " hash function not available (needs dev-python/pycrypto)") + return hashfunc_map[hashname].checksum_str(data)