title: Python 3 sign a message with key sha512
body: <p>I have the following line of code:</p>
  
  <pre><code>return hmac.new(self.Secret.upper() , message , hashlib.sha512).digest().encode("base64").replace("\n","")
  </code></pre>
  
  <p>This works fine in Python2, but not in Python 3. As soon as I try in Python 3 I get:</p>
  
  <pre><code>TypeError: key: expected bytes or bytearray, but got 'str'
  </code></pre>
  
  <p>from <code>hmac.py</code>. I've of course tried converting <code>self.Secret</code> and <code>message</code> using <code>bytes()</code> and <code>bytearray()</code>, but that hasn't worked either.</p>
  
  <p>I've also tried using <code>hashlib.sha512</code> to generate the key, but that's not very clear to me. I have a secret API key that needs to be used to sign the POST data and the only clear way to do this is using <code>hmac.new()</code>, but when I finally do get something that can be passed as a string it looks nothing like it should, and as expected I can't authenticate with it.</p>
  
  <p>Another attempt was:</p>
  
  <pre><code>sign = hashlib.sha512(message_to_sign)
  sign.update(self.Secret.upper())
  </code></pre>
  
  <p>This also produces a string that looks nothing like the working key that comes from the original code in Python 2.7.</p>
  
  <p>I've tried so many other variations as well, but none of them give me anything that works.</p>