title: Try To Code HMAC-SHA256 using C#.Net
body: <p>i try to coding HMAC-SHA256 Algorithm as Function</p>
  
  <p><strong>HMAC (K,m) = H((K ⊕ opad) ∥ H((K ⊕ ipad) ∥ m))</strong></p>
  
  <p>where</p>
  
  <ul>
  <li>H is a cryptographic hash function,</li>
  <li>K is a secret key padded to the right with extra zeros to the input block size of the hash function, or the hash of the original key if it's longer than that block size,</li>
  <li>m is the message to be authenticated,</li>
  <li>∥ denotes concatenation,</li>
  <li>⊕ denotes exclusive or (XOR),</li>
  <li>opad is the outer padding (0x5c5c5c…5c5c, one-block-long hexadecimal constant),</li>
  <li>ipad is the inner padding(0x363636…3636, one-block-long hexadecimal constant).</li>
  </ul>
  
  <p>and this my code</p>
  
  <pre><code>public static string MyHMACHash(string key , string message)
  {
      Encoding encoding = Encoding.UTF8;
      //var md = System.Security.Cryptography.MD5CryptoServiceProvider.Create();
      SHA256 hash = SHA256Managed.Create();
      byte[] trans_5C = new byte[32];
      byte[] trans_36 = new byte[32];
  
      byte[] b_key = encoding.GetBytes(key);
      // TODO: also check if key is to short
      if (b_key.Length &gt; 32)
          b_key = hash.ComputeHash(b_key);
  
      for (int i = 0; i &lt; 32; i++)
      {
          trans_5C[i] = 92;
          trans_36[i] = 54;
          if (i &lt; key.Length)
          {
              trans_5C[i] ^= b_key[i];
              trans_36[i] ^= b_key[i];
          }
      }
  
      byte[] inner = hash.ComputeHash(trans_36.Concat(encoding.GetBytes(message)).ToArray());
      var Fhash = hash.ComputeHash(trans_5C.Concat(inner).ToArray());
  
      StringBuilder sb = new StringBuilder();
      foreach (byte b in Fhash)
          sb.Append(b.ToString("x2"));
  
      string result = sb.ToString(); // = 9036a1a3f654aefeab426e9f7e17288e
      return result;
  }
  </code></pre>
  
  <p>but when i try to test this code the result <strong>Non-conforming</strong> to standard HMAC-SHA256 hashing on the standard internet web sites</p>