1 |
Add a checksum_str() method to Portage hashes and a matching function to |
2 |
make it possible to compute checksums of arbitrary bytestrings rather |
3 |
than just files. |
4 |
--- |
5 |
pym/portage/checksum.py | 29 +++++++++++++++++++++++++++++ |
6 |
1 file changed, 29 insertions(+) |
7 |
|
8 |
diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py |
9 |
index 67d6a544f..9ba251f29 100644 |
10 |
--- a/pym/portage/checksum.py |
11 |
+++ b/pym/portage/checksum.py |
12 |
@@ -59,6 +59,18 @@ class _generate_hash_function(object): |
13 |
hashfunc_map[hashtype] = self |
14 |
hashorigin_map[hashtype] = origin |
15 |
|
16 |
+ def checksum_str(self, data): |
17 |
+ """ |
18 |
+ Obtain a checksum of a byte-string. |
19 |
+ |
20 |
+ @param data: Data to hash |
21 |
+ @type data: bytes |
22 |
+ @return: The hash of the data (hex-digest) |
23 |
+ """ |
24 |
+ checksum = self._hashobject() |
25 |
+ checksum.update(data) |
26 |
+ return checksum.hexdigest() |
27 |
+ |
28 |
def checksum_file(self, filename): |
29 |
""" |
30 |
Run a checksum against a file. |
31 |
@@ -461,3 +473,20 @@ def perform_multiple_checksums(filename, hashes=["MD5"], calc_prelink=0): |
32 |
raise portage.exception.DigestException(x+" hash function not available (needs dev-python/pycrypto or >=dev-lang/python-2.5)") |
33 |
rVal[x] = perform_checksum(filename, x, calc_prelink)[0] |
34 |
return rVal |
35 |
+ |
36 |
+ |
37 |
+def checksum_str(data, hashname="MD5"): |
38 |
+ """ |
39 |
+ Run a specific checksum against a byte string. |
40 |
+ |
41 |
+ @param filename: Data to checksum |
42 |
+ @type filename: Bytes |
43 |
+ @param hashname: The type of hash function to run |
44 |
+ @type hashname: String |
45 |
+ @rtype: String |
46 |
+ @return: The hash (hex-digest) of the data |
47 |
+ """ |
48 |
+ if hashname not in hashfunc_map: |
49 |
+ raise portage.exception.DigestException(hashname + \ |
50 |
+ " hash function not available (needs dev-python/pycrypto)") |
51 |
+ return hashfunc_map[hashname].checksum_str(data) |
52 |
-- |
53 |
2.12.0 |