title: convert String to Bytes object for hmac.new() in python 3
body: <p>code example from <a href="https://www.mimecast.com/developer/documentation/downloading-siem-logs/" rel="nofollow noreferrer">https://www.mimecast.com/developer/documentation/downloading-siem-logs/</a> shows a script that can be used to download logs from a service in python 2.7.</p>
  
  <p>I am working on updating the script to be compatible with python 3 but cannot figure out how to get a string to pass into the hmac.new section for the hash generation here:</p>
  
  <pre><code>SECRET_KEY = 'SECRET KEY FOR YOUR ADMINISTRATOR'
  secret_key = SECRET_KEY
  def create_signature(data_to_sign, secret_key):
          digest = hmac.new(secret_key.decode("base64"), data_to_sign, digestmod=hashlib.sha1).digest()
          return base64.encodestring(digest).rstrip()
  </code></pre>
  
  <p>trying to run this as-is results in "LookupError, 'base64' is not a text encoding; use codecs.decode() to handle arbitrary codecs"</p>
  
  <p>I was trying to convert the variable to bytes prior to or inside this function by using the </p>
  
  <p><code>base64.b64decode</code>
  or <code>bytes(secret_key)</code></p>
  
  <p>functions but that results in "TypeError, Unicode-objects must be encoded before hashing" and I'm just not finding a lot of information on how to get a string variable to pass into hmac.new().</p>
  
  <p>I am pretty sure this is related to the change between python 2 and 3 where strings are stored as unicode in 3 where it was raw data in 2, but I'm not familiar enough with these encodings to understand how to properly translate them when passing them around</p>