title: Trouble creating auth string using PHP HMAC SHA1 and Base64
body: <p>So I am working with this API and using Laravel, and I am trying to build an auth string. This is the documentation I was given, but I am having a little trouble as this is something relatively new to me.</p>
  
  <p>Here are the auth instructions:</p>
  
  <blockquote>
    <p>The authentication parameter is a string and it can calculated by the
    caller or the caller can choose to save this value as a parameter
    together with connection ID and API key. </p>
    
    <p>The authentication is a base64 string of a HMAC SHA1 hash. This is
    computed by using the binary of API Key in in</p>
    
    ########################## format in all lower case and UTF8 encoding as the key and computer HMAC SHA1 hash on the binary of
    
    <p>Connection ID in ################################ format in all lower
    case and UTF8 encoding. </p>
    
    <p>The result binary hash is then base64 encoded and the text result is
    what should be passed as the authentication parameter. In C# the code
    to calculate the authentication may look like:</p>
  
  <pre><code>HMACSHA1 hmac = new HMACSHA1(
    UTF8Encoding.UTF8.GetBytes(apiKey.ToString("N").ToLower())
  );
  string authentication = Convert.ToBase64String(
    hmac.ComputeHash(
      UTF8Encoding.UTF8.GetBytes(connectionId.ToString("N").ToLower())
    )
  );
  </code></pre>
    
    <p>As an example the following credentials:</p>
    
    <p>Connection ID: <code>5fecbc200f0e4a7cbf41040e11047e56</code> </p>
    
    <p>API Key: <code>2de51c4fd0f04b9fabeb95225e87da70</code></p>
    
    <p>Should result in a computed authentication value of
    <code>m5/Vc1RzhUETQvEtx/JdIglQpTg=</code></p>
  </blockquote>
  
  <p>So what i have been trying is:</p>
  
  <pre><code>$a = strtolower('5fecbc200f0e4a7cbf41040e11047e56');
  $b = strtolower('2de51c4fd0f04b9fabeb95225e87da70');
  $z = hash_hmac("sha1", utf8_encode(decbin($b)), utf8_encode(decbin($a)), true);
  dd(base64_encode($z));
  </code></pre>
  
  <p>Which outputs <code>QjG3kzUs7U1UukNd++3t24pBWNk=</code></p>
  
  <p>I have tried a few more variations, but I am just lost on this one. First time really decoding or encoding anything. Would greatly appreciate any tips, ideas, or snippets that can help me figure this out. Already spent a few hours on this and it's bugging me..</p>