Python3 and hmac . How to handle string not being binary

后端 未结 3 1701
自闭症患者
自闭症患者 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:53

    try

    codecs.encode()

    which can be used both in python2.7.12 and 3.5.2

    import hashlib
    import codecs
    import hmac
    
    a = "aaaaaaa"
    b = "bbbbbbb"
    hmac.new(codecs.encode(a), msg=codecs.encode(b), digestmod=hashlib.sha256).hexdigest()
    

提交回复
热议问题