commit 7ac4b8ea8605470371d78ba6e3c58731c9549c57 Author: Damian Johnson atagar@torproject.org Date: Tue Jan 14 14:29:42 2020 -0800
Normalize getHMAC inputs
Calculating the hmac requires bytes. This fixes...
Traceback (most recent call last): File "/home/atagar/Desktop/tor/bridgedb/bridgedb/test/test_https_distributor.py", line 152, in test_HTTPSDistributor_prepopulateRings_without_proxies dist = distributor.HTTPSDistributor(3, self.key) File "/home/atagar/Desktop/tor/bridgedb/bridgedb/distributors/https/distributor.py", line 93, in __init__ key2 = getHMAC(key, "Assign-Bridges-To-Rings") File "/home/atagar/Desktop/tor/bridgedb/bridgedb/crypto.py", line 193, in getHMAC h = hmac.new(key, value, digestmod=DIGESTMOD) File "/usr/lib/python3.5/hmac.py", line 144, in new return HMAC(key, msg, digestmod) File "/usr/lib/python3.5/hmac.py", line 42, in __init__ raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) builtins.TypeError: key: expected bytes or bytearray, but got 'str'
Test results changed as follows...
before: FAILED (skips=109, failures=22, errors=318, successes=531) after: FAILED (skips=109, failures=21, errors=283, successes=567) --- bridgedb/crypto.py | 11 +++++++++++ 1 file changed, 11 insertions(+)
diff --git a/bridgedb/crypto.py b/bridgedb/crypto.py index b39c659..bd3d783 100644 --- a/bridgedb/crypto.py +++ b/bridgedb/crypto.py @@ -190,6 +190,12 @@ def getKey(filename):
def getHMAC(key, value): """Return the HMAC of **value** using the **key**.""" + + # normalize inputs to be bytes + + key = key.encode('utf-8') if isinstance(key, str) else key + value = value.encode('utf-8') if isinstance(value, str) else value + h = hmac.new(key, value, digestmod=DIGESTMOD) return h.digest()
@@ -200,14 +206,19 @@ def getHMACFunc(key, hex=True): :rtype: callable :returns: A function which can be uses to generate HMACs. """ + + key = key.encode('utf-8') if isinstance(key, str) else key h = hmac.new(key, digestmod=DIGESTMOD) + def hmac_fn(value): + value = value.encode('utf-8') if isinstance(value, str) else value h_tmp = h.copy() h_tmp.update(value) if hex: return h_tmp.hexdigest() else: return h_tmp.digest() + return hmac_fn
def removePKCS1Padding(message):
tor-commits@lists.torproject.org