Python3 and hmac . How to handle string not being binary

后端 未结 3 1713
自闭症患者
自闭症患者 2020-12-08 13:08

I had a script in Python2 that was working great.

def _generate_signature(data):
   return hmac.new(\'key\', data, hashlib.sha256).hexdigest()
3条回答
  •  感动是毒
    2020-12-08 13:58

    You can use bytes literal: b'key'

    def _generate_signature(data):
        return hmac.new(b'key', data, hashlib.sha256).hexdigest()
    

    In addition to that, make sure data is also bytes. For example, if it is read from file, you need to use binary mode (rb) when opening the file.

提交回复
热议问题