Skip to content
Advertisement

Port hmac.new().digest() module from Python 2.7 to 3.7

I have been struggling with this for hours. I have the following production code (parsed out for simplicity) that runs just fine in Python 2.7:

JavaScript

The output is a string like so:

JavaScript

But when I run this with Python3.7, I get the following error:

JavaScript

After a quite a bit of research I understood that hmac has changed in 3.4 and beyond. Therefore I redid my code to the following:

JavaScript

But the output I get is completely different!

JavaScript

How do I correctly port this code to python3.7 so I get the exact same output as 2.7?

Thanks in advance!

Advertisement

Answer

Thanks to Josh Lee for his answer in UnicodeDecodeError, invalid continuation byte

His suggestion about using ‘latin-1’ to decode the digest output solved the problem for me!

Here’s how my code looks in Python 3.7 now and gives me the exact same output as my code in Python 2.7:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement