Avevo uno script in Python2 che funzionava alla grande.Python3 e hmac. Come gestire la stringa non essendo binario
def _generate_signature(data):
return hmac.new('key', data, hashlib.sha256).hexdigest()
Dove data era l'uscita di json.dumps
.
Ora, se provo a eseguire lo stesso tipo di codice in Python 3, ottengo il seguente:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/hmac.py", line 144, in new
return HMAC(key, msg, digestmod)
File "/usr/lib/python3.4/hmac.py", line 42, in __init__
raise TypeError("key: expected bytes or bytearray, but got %r" %type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'
Se provo qualcosa come trasformare la chiave per byte in questo modo:
bytes('key')
ottengo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
che sto ancora lottando per capire le codifiche in Python 3.
come posso usare byte letterale per un param? –
Con il prefisso 'b' davanti alla stringa letterale. ''chiave'': stringa,' b'key'': byte. – falsetru