Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH 09/14] portage.checksum: Stop exposing global hash variables
Date: Sun, 12 Mar 2017 19:03:16
Message-Id: 20170312190011.3234-10-mgorny@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCHES] portage.checksum hacking, pt. 4 by "Michał Górny"
1 Stop exposing global variables such as 'md5hash'. Those are not used
2 anywhere anymore, exposing them makes the code more complex and makes it
3 easy to accidentally fail to set them properly (this happened already
4 for SHA3). Instead, rely on them being inserted into hashfunc_map.
5 ---
6 pym/portage/checksum.py | 42 ++++++++++++++++++++----------------------
7 1 file changed, 20 insertions(+), 22 deletions(-)
8
9 diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
10 index 68ed44fa9..659012cdc 100644
11 --- a/pym/portage/checksum.py
12 +++ b/pym/portage/checksum.py
13 @@ -102,11 +102,10 @@ class _generate_hash_function(object):
14 # WHIRLPOOL available.
15 try:
16 import mhash
17 - for local_name, hash_name in (("rmd160", "ripemd160"), ("whirlpool", "whirlpool")):
18 - if hasattr(mhash, 'MHASH_%s' % local_name.upper()):
19 - globals()['%shash' % local_name] = \
20 - _generate_hash_function(local_name.upper(), \
21 - functools.partial(mhash.MHASH, getattr(mhash, 'MHASH_%s' % hash_name.upper())), \
22 + for local_name, hash_name in (("RMD160", "RIPEMD160"), ("WHIRLPOOL", "WHIRLPOOL")):
23 + if hasattr(mhash, 'MHASH_%s' % hash_name):
24 + _generate_hash_function(local_name,
25 + functools.partial(mhash.MHASH, getattr(mhash, 'MHASH_%s' % hash_name)),
26 origin='mhash')
27 except ImportError:
28 pass
29 @@ -118,7 +117,7 @@ try:
30 from Crypto.Hash import RIPEMD
31 rmd160hash_ = getattr(RIPEMD, 'new', None)
32 if rmd160hash_ is not None:
33 - rmd160hash = _generate_hash_function("RMD160",
34 + _generate_hash_function("RMD160",
35 rmd160hash_, origin="pycrypto")
36 except ImportError:
37 pass
38 @@ -129,19 +128,19 @@ try:
39
40 blake2bhash_ = getattr(BLAKE2b, 'new', None)
41 if blake2bhash_ is not None:
42 - blake2bhash = _generate_hash_function("BLAKE2B",
43 + _generate_hash_function("BLAKE2B",
44 functools.partial(blake2bhash_, digest_bytes=64), origin="pycrypto")
45 blake2shash_ = getattr(BLAKE2s, 'new', None)
46 if blake2shash_ is not None:
47 - blake2shash = _generate_hash_function("BLAKE2S",
48 + _generate_hash_function("BLAKE2S",
49 functools.partial(blake2shash_, digest_bytes=32), origin="pycrypto")
50 sha3_256hash_ = getattr(SHA3_256, 'new', None)
51 if sha3_256hash_ is not None:
52 - sha3_256hash = _generate_hash_function("SHA3_256",
53 + _generate_hash_function("SHA3_256",
54 sha3_256hash_, origin="pycrypto")
55 sha3_512hash_ = getattr(SHA3_512, 'new', None)
56 if sha3_512hash_ is not None:
57 - sha3_512hash = _generate_hash_function("SHA3_512",
58 + _generate_hash_function("SHA3_512",
59 sha3_512hash_, origin="pycrypto")
60 except ImportError:
61 pass
62 @@ -150,20 +149,20 @@ except ImportError:
63 try:
64 import sha3
65
66 - sha3_256hash = _generate_hash_function("SHA3_256", sha3.sha3_256, origin="pysha3")
67 - sha3_512hash = _generate_hash_function("SHA3_512", sha3.sha3_512, origin="pysha3")
68 + _generate_hash_function("SHA3_256", sha3.sha3_256, origin="pysha3")
69 + _generate_hash_function("SHA3_512", sha3.sha3_512, origin="pysha3")
70 except ImportError:
71 pass
72
73 # Use hashlib from python-2.5 if available and prefer it over pycrypto and internal fallbacks.
74 # Need special handling for RMD160/WHIRLPOOL as they may not always be provided by hashlib.
75 -md5hash = _generate_hash_function("MD5", hashlib.md5, origin="hashlib")
76 -sha1hash = _generate_hash_function("SHA1", hashlib.sha1, origin="hashlib")
77 -sha256hash = _generate_hash_function("SHA256", hashlib.sha256, origin="hashlib")
78 -sha512hash = _generate_hash_function("SHA512", hashlib.sha512, origin="hashlib")
79 +_generate_hash_function("MD5", hashlib.md5, origin="hashlib")
80 +_generate_hash_function("SHA1", hashlib.sha1, origin="hashlib")
81 +_generate_hash_function("SHA256", hashlib.sha256, origin="hashlib")
82 +_generate_hash_function("SHA512", hashlib.sha512, origin="hashlib")
83 for local_name, hash_name in (
84 - ("rmd160", "ripemd160"),
85 - ("whirlpool", "whirlpool"),
86 + ("RMD160", "ripemd160"),
87 + ("WHIRLPOOL", "whirlpool"),
88 # available since Python 3.6
89 ("BLAKE2B", "blake2b"),
90 ("BLAKE2S", "blake2s"),
91 @@ -175,9 +174,8 @@ for local_name, hash_name in (
92 except ValueError:
93 pass
94 else:
95 - globals()['%shash' % local_name] = \
96 - _generate_hash_function(local_name.upper(), \
97 - functools.partial(hashlib.new, hash_name), \
98 + _generate_hash_function(local_name,
99 + functools.partial(hashlib.new, hash_name),
100 origin='hashlib')
101
102 _whirlpool_unaccelerated = False
103 @@ -185,7 +183,7 @@ if "WHIRLPOOL" not in hashfunc_map:
104 # Bundled WHIRLPOOL implementation
105 _whirlpool_unaccelerated = True
106 from portage.util.whirlpool import new as _new_whirlpool
107 - whirlpoolhash = _generate_hash_function("WHIRLPOOL", _new_whirlpool, origin="bundled")
108 + _generate_hash_function("WHIRLPOOL", _new_whirlpool, origin="bundled")
109
110
111 # There is only one implementation for size
112 --
113 2.12.0