TypeError: key: expected bytes or bytearray, but got 'str'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: key: expected bytes or bytearray, but got 'str' #

The Python "TypeError: key: expected bytes or bytearray, but got 'str'" occurs when we pass a string key to the hmac.new() method. To solve the error, encode the string into a bytes object, by using the encode() method or prefix the string with b.

typeerror key expected bytes or bytearray but got str

Here is an example of how the error occurs.

main.py
import hmac import hashlib signature = hmac.new('my_secret_key', b'my_message', digestmod=hashlib.sha256).hexdigest() # ⛔️ TypeError: key: expected bytes or bytearray, but got 'str' print(signature)

The first argument we passed to hmac.new method must be a bytes or bytearray object that represents the secret key.

We passed as string for the argument which caused the error.

You can use the encode() method to encode the string to a bytes object.

main.py
import hmac import hashlib signature = hmac.new('my_secret_key'.encode('utf-8'), b'my_message', digestmod=hashlib.sha256).hexdigest() # 👇️ dd12963782c45f4c46708bca6d53c9631d2bfbd53b0e086d76512b38cb8715be print(signature)

The str.encode method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

The second argument the hmac method takes is the msg and can be of any type supported by hashlib (e.g. a bytes object).

If you have a string literal and not a string stored in a variable, you can simply prefix the string with b to turn it into a bytes object.

main.py
import hmac import hashlib signature = hmac.new(b'my_secret_key', b'my_message', digestmod=hashlib.sha256).hexdigest() # 👇️ dd12963782c45f4c46708bca6d53c9631d2bfbd53b0e086d76512b38cb8715be print(signature)

Otherwise, you can call the encode() method to encode both strings into bytes objects.

main.py
import hmac import hashlib signature = hmac.new('my_secret_key'.encode('utf-8'), 'my_message'.encode('utf-8'), digestmod=hashlib.sha256).hexdigest() # 👇️ dd12963782c45f4c46708bca6d53c9631d2bfbd53b0e086d76512b38cb8715be print(signature)

The third argument the hmac.new method takes is the digestmod - the digest constructor for the HMAC object to use. This argument is required.

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_bytes = 'hello'.encode('utf-8') print(type(my_bytes)) # 👉️ <class 'bytes'> print(isinstance(my_bytes, bytes)) # 👉️ True my_str = 'hello' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Conclusion #

The Python "TypeError: key: expected bytes or bytearray, but got 'str'" occurs when we pass a string key to the hmac.new() method. To solve the error, encode the string into a bytes object, by using the encode() method or prefix the string with b.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.