References are not available in the final program

0

I'm validating some functions created in python, I have a code that creates some functions. When I run the program with the functions (test_client.py), I get run-time error. auth_utils.py:

from hashlib import sha256
try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode


def create_signature(secret_key, data=None):
    if data is None:
        data = {}
    url_encoded_data = urlencode(sorted(data.items()))
    hashed_data = sha256(secret_key + url_encoded_data)
    return hashed_data.hexdigest()

test_client.py:

from auth_utils import create_signature
from time import time


API_KEY = "kaakAYJ58EouuGW2"
API_SECRET = "HRN4Ig7BMB8xY9qVLJA7Ylzy"
DATA = {
    'first_name': 'Jonh',

execution error:

  

$ python test_headers.py Traceback (most recent call last): File   "test_headers.py", line 13, in       header="Authorization Voxy {}: {}". format (API_KEY, create_signature (API_SECRET, DATA)) File   "F: \ xampp \ htdocs \ proj \ voxy \ auth_utils.py", line 12, in   create_signature       hashed_data = sha256 (secret_key + url_encoded_data) TypeError: Unicode-objects must be encoded before hashing

    
asked by anonymous 19.02.2017 / 03:49

1 answer

2

By error, you are just missing the encoding of the objects when you execute hash . Try to apply the encoding, such as:

sha256(str(secret_key + url_encoded_data).encode('utf-8'))

Getting:

def create_signature(secret_key, data=None):
    if data is None:
        data = {}
    url_encoded_data = urlencode(sorted(data.items()))
    hashed_data = sha256(str(secret_key + url_encoded_data).encode('utf-8'))
    return hashed_data.hexdigest()

See working at Repl.it .

    
19.02.2017 / 04:30