Search code examples
pythonbytesha

Strange representation of Bytes in Python


I've a simple program that hashes a string.

from Crypto.Hash import SHA512

PAYLOAD = "Hello world"

def sha(message):
    h = SHA512.new()
    h.update(message.encode('ascii'))
    return h.digest()


if __name__ == '__main__':
    print(sha(PAYLOAD))
    print(type(sha(PAYLOAD)))

This is the output,

b'\xb7\xf7\x83\xba\xed\x82\x97\xf0\xdb\x91tb\x18O\xf4\xf0\x8ei\xc2\xd5\xe5\xf7\x9a\x94&\x00\xf9r_X\xce\x1f)\xc1\x819\xbf\x80\xb0l\x0f\xff+\xdd4s\x84R\xec\xf4\x0cH\x8c"\xa7\xe3\xd8\x0c\xdfo\x9c\x1c\rG'

I fail to understand how is this a byte array?


Solution

  • digest() is documented to return a bytes object, and a bytes object is exactly what you're getting: https://docs.python.org/3/library/stdtypes.html#bytes-objects

    bytearray is similar to bytes but isn't quite the same (it's basically a mutable version of bytes).

    If you need a list of integers, you could simply call list()

    >>> list(b'\xb7\xf7\x83\xba\xed\x82\x97\xf0\xdb\x91tb\x18O\xf4\xf0\x8ei\xc2\xd5\xe5\xf7\x9a\x94&\x00\xf9r_X\xce\x1f)\xc1\x819\xbf\x80\xb0l\x0f\xff+\xdd4s\x84R\xec\xf4\x0cH\x8c"\xa7\xe3\xd8\x0c\xdfo\x9c\x1c\rG')
    [183, 247, 131, 186, 237, 130, 151, 240, 219, 145, 116, 98, 24, 79, 244, 240, 142, 105, 194, 213, 229, 247, 154, 148, 38, 0, 249, 114, 95, 88, 206, 31, 41, 193, 129, 57, 191, 128, 176, 108, 15, 255, 43, 221, 52, 115, 132, 82, 236, 244, 12, 72, 140, 34, 167, 227, 216, 12, 223, 111, 156, 28, 13, 71]
    

    If you need a hex representation of the SHA digest, there's hexdigest().