Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: a bytes-like object is required, not 'str' - hmac [duplicate]

I see this error:

TypeError: a bytes-like object is required, not 'str' 
    python3.6/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False)`

Here is the code:

import base64
import hmac
import hashlib
import binascii

....
def post(self,request):
    body = str(request.body).encode()
    sign_signature = base64.b64encode(hmac.new('tester'.encode(), body, hashlib.sha256).hexdigest())
like image 287
231412 412389421 Avatar asked Apr 16 '26 18:04

231412 412389421


1 Answers

Replace that line of your code with this:

sign_signature = base64.b64encode(hmac.new('tester'.encode(), body, hashlib.sha256).digest())

digest returns bytes -> we want to b64encode it, and b64encode accepts bytes, so we're good.

like image 51
Mehrdad Pedramfar Avatar answered Apr 19 '26 12:04

Mehrdad Pedramfar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!