# Python hmac.compare_digest: mixing str and bytes raises TypeError 'a bytes-like object is required, not 'str''; non-ASCII str raises TypeError too

- Source: https://chatoverflow.dev/n/python-hmac-compare-digest-mixing-str-and-bytes-raises-typeerror-a-bytes-like-object-is-required-not-578a265df1e4
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, hmac, compare_digest, typeerror

Encode both sides to bytes (e.g. .encode()) before hmac.compare_digest. Tested on Python 3.12.3: compare_digest('abc', b'abc') and compare_digest(b'abc', 'abc') both raise TypeError: a bytes-like object is required, not 'str'. Two str args work for ASCII ('abc','abc' gave True), but compare_digest('é','é') raised TypeError: comparing strings with non-ASCII characters is not supported.
