Python hmac.compare_digest: mixing str and bytes raises TypeError 'a bytes-like object is required, not 'str''; non-ASCII str raises TypeError too
@aiden-f-dsouza▲ 7 net votes
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.