1 |
commit: f27473d04e6dee44983d1e5ac32ea9d4d375b5a2 |
2 |
Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Oct 1 07:40:51 2011 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Oct 2 05:24:58 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=f27473d0 |
7 |
|
8 |
Refactor RMD160 hashlib code for less-hardcoding |
9 |
|
10 |
To be used shortly for WHIRLPOOL as well as RMD160. |
11 |
|
12 |
Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org> |
13 |
|
14 |
--- |
15 |
pym/portage/checksum.py | 21 ++++++++++++--------- |
16 |
1 files changed, 12 insertions(+), 9 deletions(-) |
17 |
|
18 |
diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py |
19 |
index 52e45d3..8daefbc 100644 |
20 |
--- a/pym/portage/checksum.py |
21 |
+++ b/pym/portage/checksum.py |
22 |
@@ -87,19 +87,22 @@ except ImportError as e: |
23 |
# Use hashlib from python-2.5 if available and prefer it over pycrypto and internal fallbacks. |
24 |
# Need special handling for RMD160 as it may not always be provided by hashlib. |
25 |
try: |
26 |
- import hashlib |
27 |
+ import hashlib, functools |
28 |
|
29 |
md5hash = _generate_hash_function("MD5", hashlib.md5, origin="hashlib") |
30 |
sha1hash = _generate_hash_function("SHA1", hashlib.sha1, origin="hashlib") |
31 |
sha256hash = _generate_hash_function("SHA256", hashlib.sha256, origin="hashlib") |
32 |
- try: |
33 |
- hashlib.new('ripemd160') |
34 |
- except ValueError: |
35 |
- pass |
36 |
- else: |
37 |
- def rmd160(): |
38 |
- return hashlib.new('ripemd160') |
39 |
- rmd160hash = _generate_hash_function("RMD160", rmd160, origin="hashlib") |
40 |
+ for local_name, hash_name in (("rmd160", "ripemd160"), ): |
41 |
+ try: |
42 |
+ hashlib.new(hash_name) |
43 |
+ except ValueError: |
44 |
+ pass |
45 |
+ else: |
46 |
+ globals()['%shash' % local_name] = \ |
47 |
+ _generate_hash_function(local_name.upper(), \ |
48 |
+ functools.partial(hashlib.new, hash_name), \ |
49 |
+ origin='hashlib') |
50 |
+ |
51 |
except ImportError as e: |
52 |
pass |