title: www.crypto.com authenticating, python HMAC-SHA256
body: <p>Trying to authenticate to crypto.com but cant seem to get it to work... Been trying few days now and getting really frustrated, any help? Their api docs @ <a href="https://exchange-docs.crypto.com/?python#digital-signature" rel="nofollow noreferrer">https://exchange-docs.crypto.com/?python#digital-signature</a></p>
  <p>Heres how to do it + sample code, Im stuck..</p>
  <hr />
  <p>The authentication is based on the pairing of the API Key, along with the HMAC-SHA256 hash of the request parameters using the API Secret as the cryptographic key.</p>
  <p>The algorithm for generating the HMAC-SHA256 signature is as follows:</p>
  <p>If &quot;params&quot; exist in the request, sort the request parameter keys in ascending order.</p>
  <p>Combine all the ordered parameter keys as key + value (no spaces, no delimiters). Let's call this the parameter string</p>
  <p>Next, do the following: method + id + api_key + parameter string + nonce</p>
  <p>Use HMAC-SHA256 to hash the above using the API Secret as the cryptographic key</p>
  <p>Encode the output as a hex string -- this is your Digital Signature</p>
  <pre><code>import hmac
  import hashlib
  import json
  import requests
  import time
  
  API_KEY = &quot;API_KEY&quot;
  SECRET_KEY = &quot;SECRET_KEY&quot;
  
  req = {
    &quot;id&quot;: 11,
    &quot;method&quot;: &quot;private/get-order-detail&quot;,
    &quot;api_key&quot;: API_KEY,
    &quot;params&quot;: {
      &quot;order_id&quot;: &quot;337843775021233500&quot;,
    },
    &quot;nonce&quot;: int(time.time() * 1000)
  };
  
  # First ensure the params are alphabetically sorted by key
  paramString = &quot;&quot;
  
  if &quot;params&quot; in req:
    for key in req['params']:
      paramString += key
      paramString += str(req['params'][key])
  
  sigPayload = req['method'] + str(req['id']) + req['api_key'] + paramString + str(req['nonce'])
  
  request['sig'] = hmac.new(
    bytes(str(SECRET_KEY), 'utf-8'),
    msg=bytes(sigPayload, 'utf-8'),
    digestmod=hashlib.sha256
  ).hexdigest()
  </code></pre>