0

so i am trying to get this hash 69F6617707D9442940C39BEBFA0B4459CEBF6CD5E610EA1FF44B341E291654A9 which can me made on https://caligatio.github.io/jsSHA/. The key is K = E74A540FA07C4DB1B46421126DF7AD36 , Message is T= 537707AD486F07AD75CCF7B1F7FEA6F75871FCF6DC755923C8EE7C375C21EAC51BD97C51C69F395B and the generated hash via my code is SHA256 (T) = 6a2783d8eb0237e015f7dcd27fb2d2a85dd637220433dffcdc31198670f6ecac This is incorrect Hash. This is my code can someone explain what is wrong here thanks.

static void Main(string[] args)
    {
        var key = "E74A540FA07C4DB1B46421126DF7AD36";
        string message = "537707AD486F07AD75CCF7B1F7FEA6F75871FCF6DC755923C8EE7C375C21EAC51BD97C51C69F395B";
        var encodingCred = new System.Text.ASCIIEncoding();
        var encodingKey = new System.Text.ASCIIEncoding();
        byte[] keyByte = StringToByteArray(key);
        byte[] credentialsBytes = encodingCred.GetBytes(message);
        using (var hmacsha256 = new HMACSHA256(keyByte))
        {
            byte[] hashmessage = hmacsha256.ComputeHash(credentialsBytes);
            string hash = BitConverter.ToString(hashmessage).Replace("-", string.Empty).ToLower();
            Console.WriteLine("HASH: " + hash);
        }
        Console.ReadKey();
    }
    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }

In C this is valid

void test_keyed_hmac()
{
hmac_sha256 hmac;
sha256 sha;
uint8_t sha_digest[32] = { 0 };

unsigned char key[] = "\xE7\x4A\x54\x0F\xA0\x7C\x4D\xB1\xB4\x64\x21\x12\x6D\xF7\xAD\x36";

//unsigned char key[] = "E74A540FA07C4DB1B46421126DF7AD36";
unsigned char data[] = "\x53\x77\x07\xAD\x48\x6F\x07\xAD\x75\xCC\xF7\xB1\xF7\xFE\xA6\xF7\x58\x71\xFC\xF6\xDC\x75\x59\x23\xC8\xEE\x7C\x37\x5C\x21\xEA\xC5\x1B\xD9\x7C\x51\xC6\x9F\x39\x5B";
//unsigned char data[] = "537707AD486F07AD75CCF7B1F7FEA6F75871FCF6DC755923C8EE7C375C21EAC51BD97C51C69F395B";


printf("Data length %d \n", strlen(data));
hmac_sha256_initialize(&hmac, key, strlen(key));


// Finalize the HMAC-SHA256 digest and output its value. 
hmac_sha256_finalize(&hmac, data, strlen(data));
for (int i = 0; i < 32; ++i) {
//  Cast added by RKW to get format specifier to work as expected  
printf("%x", (unsigned long)hmac.digest[i]);
}
putchar('\n');

}

I need something like this in c# that gives the valid hash mentioned above in second line

  • Did the padding mode change? The default padding method is different between c# and java. Padding takes the key and adds random bit to make key fixed size. So if a key has 120 bits it would make 128 bits and fill the 8 bits with random data. – jdweng Jun 24 '20 at 16:53
  • Well i don't think so, one of my friend implemented it with the same number of bits in C and got the exact output but C# is not same as C. Do you think it should have printed the right result? As for padded bits I'll look into it again but i think that shouldn't be the case. – Muhammad Bilal Jun 24 '20 at 18:33
  • Was c code implemented with the managed Net Library? – jdweng Jun 24 '20 at 23:06
  • Well this i did not know – Muhammad Bilal Jun 25 '20 at 07:37
  • Kindly see the C code above. the gives the valid hash. @jdweng – Muhammad Bilal Jun 25 '20 at 10:18
  • Don't you need : byte[] credentialsBytes = StringToByteArray(message); – jdweng Jun 25 '20 at 11:03

0 Answers0