Skip to content

Commit d7c66bb

Browse files
committed
Made sure hmac creation works on bytes
1 parent 3498c94 commit d7c66bb

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

laterpay/compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ def stringify(value):
3838
elif six.PY2 and isinstance(value, six.text_type):
3939
value = value.encode('utf-8')
4040
return value
41+
42+
43+
def byteify(value):
44+
"""
45+
Convert ``value`` into a byte-string.
46+
"""
47+
if isinstance(value, six.text_type):
48+
return value.encode('utf-8')
49+
return value

laterpay/signing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ def create_HMAC(HMAC_secret, *parts):
4040
This function should probably not be part of the public API, and thus will
4141
be deprecated in a future release to be replaced with a internal function.
4242
"""
43-
authcode = hmac.new(six.b(HMAC_secret), digestmod=hashlib.sha224)
43+
authcode = hmac.new(compat.byteify(HMAC_secret), digestmod=hashlib.sha224)
4444
for part in parts:
45-
authcode.update(six.b(part))
46-
return authcode.hexdigest()
45+
authcode.update(compat.byteify(part))
46+
return compat.stringify(authcode.hexdigest())
4747

4848

4949
def sort_params(param_dict):

tests/test_signing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ def test_sign(self):
106106
'346f3d53ad762f3ed3fb7f2427dec2bbfaf0338bb7f91f0460aff15c',
107107
)
108108

109+
def test_sign_unicode_secret(self):
110+
params = {
111+
u'parĄm1': u'valuĘ',
112+
'param2': ['value2', 'value3'],
113+
'hmac': 'will-be-removed',
114+
'gettoken': 'will-be-removed-too',
115+
}
116+
url = u'https://endpoint.com/api'
117+
118+
secret = u'☃🐍' # unicode is what we usually get from api/db..
119+
120+
mac = signing.sign(secret, params, url)
121+
122+
# sha224 hmac
123+
self.assertEqual(
124+
mac,
125+
'635cef6498fc5f1a829275cc1b24a191d5267d6023034e3e0953e4c6',
126+
)
127+
109128
def test_verify_str_signature(self):
110129
params = {
111130
u'parĄm1': u'valuĘ',

0 commit comments

Comments
 (0)