Python3 and hmac . How to handle string not being binary

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

    Not to resurrect an old question but I did want to add something I feel is missing from this answer, to which I had trouble finding an appropriate explanation/example of anywhere else:

    Aquiles Carattino was pretty close with his attempt at converting the string to bytes, but was missing the second argument, the encoding of the string to be converted to bytes.

    If someone would like to convert a string to bytes through some other means than static assignment (such as reading from a config file or a DB), the following should work:

    (Python 3+ only, not compatible with Python 2)

    import hmac, hashlib
    
    def _generate_signature(data):
      key = 'key' # Defined as a simple string.
      key_bytes= bytes(key , 'latin-1') # Commonly 'latin-1' or 'utf-8'
      data_bytes = bytes(data, 'latin-1') # Assumes `data` is also a string.
      return hmac.new(key_bytes, data_bytes , hashlib.sha256).hexdigest()
    
    print(
      _generate_signature('this is my string of data')
    )
    
    0 讨论(0)
  • 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()
    

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题