what "type" is an SHA-256 result, and how do I most easily convert it into a string?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

what "type" is an SHA-256 result, and how do I most easily convert it into a string?

Post by WhiteHare » Sun Oct 28, 2018 4:03 am

The trouble is,

Code: Select all

>>> myHash
b'\x93\x1d\x93HP\xf6,.2\x856\x0e\t\x80\xed\xd9\xaf\rc\xff\xbb9\xf6~\xa8\xca\xea\x9ca\xf2\xbd\xd4'
is not a string, as confirmed by:

Code: Select all

>>> isinstance(myHash, str)
False
but it doesn't seem to behave like a number either:

Code: Select all

>>> print("0x{:02X}".format(myHash))
Traceback (most recent call last):
  File "<stdin>", in <module>
ValueError: '=' alignment not allowed in string format specifier
Whatever it is, how do I convert it into a string?

I seem to make some progress if I hexlify it:

Code: Select all

>>> import ubinascii
>>> hexHash=ubinascii.hexlify(myHash)
>>> hexHash
b'931d934850f62c2e3285360e0980edd9af0d63ffbb39f67ea8caea9c61f2bdd4'
but that also turns out to be neither a string nor a number, at least if the same tests are applied.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: what "type" is an SHA-256 result, and how do I most easily convert it into a string?

Post by jickster » Sun Oct 28, 2018 5:25 am

It’s a byteareay


Sent from my iPhone using Tapatalk Pro

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: what "type" is an SHA-256 result, and how do I most easily convert it into a string?

Post by Christian Walther » Sun Oct 28, 2018 10:41 am

It’s actually a bytes, not a bytearray (former is immutable, latter is mutable). Try

Code: Select all

type(myHash)
and read https://docs.python.org/3/library/stdtypes.html to learn about Python’s built-in types.

You didn’t specify what your resulting string should be, but one way of converting your hexHash (a bytes too) into a string is

Code: Select all

str(hexHash, 'ascii')
There is also

Code: Select all

hexHash.decode()
that does the same thing, but it’s not implemented in all ports.

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: what "type" is an SHA-256 result, and how do I most easily convert it into a string?

Post by WhiteHare » Sun Oct 28, 2018 11:52 am

Christian Walther wrote:
Sun Oct 28, 2018 10:41 am
It’s actually a bytes, not a bytearray (former is immutable, latter is mutable). Try

Code: Select all

type(myHash)
and read https://docs.python.org/3/library/stdtypes.html to learn about Python’s built-in types.

You didn’t specify what your resulting string should be, but one way of converting your hexHash (a bytes too) into a string is

Code: Select all

str(hexHash, 'ascii')
There is also

Code: Select all

hexHash.decode()
that does the same thing, but it’s not implemented in all ports.
Perfect! Thank you for your quick response. You're my hero. :D

Code: Select all

>>> type(myHash)
<class 'bytes'>
>>> str(hexHash, 'ascii')
'931d934850f62c2e3285360e0980edd9af0d63ffbb39f67ea8caea9c61f2bdd4'
>>> hexHash.decode()
'931d934850f62c2e3285360e0980edd9af0d63ffbb39f67ea8caea9c61f2bdd4'
>>>

Post Reply