Convert Python Code to C# Code (generate HMAC)

Question:

In C#, I have to create the MD5 hmac token based on following python code.

from hashlib import md5 

trans_5C = "".join(chr(x ^ 0x5c) for x in xrange(256))
trans_36 = "".join(chr(x ^ 0x36) for x in xrange(256))
blocksize = md5().block_size

def hmac_md5(key, msg):
    if len(key) > blocksize:
        key = md5(key).digest()
    key += chr(0) * (blocksize - len(key))
    o_key_pad = key.translate(trans_5C)
    i_key_pad = key.translate(trans_36)
    return md5(o_key_pad + md5(i_key_pad + msg).digest())

if __name__ == "__main__":
    h = hmac_md5("9T5zhB4sTNGxMJ-iDdO-Ow"+"8rdp7erdig0m6aa72lhanvuk01"+"pizza1", "1387797294")
    print h.hexdigest()  # 9036a1a3f654aefeab426e9f7e17288e

As in windowsphone8 we don’t have MD5 implementation so i downloaded the MD5 Class from here thanks to jeff. It works generating MD5 but my problem is still here. The above Python code generates the exact required token 9036a1a3f654aefeab426e9f7e17288e, but mine generates 8280c9a3804b53792324b62363fc22fd.

Can anyone translate the python code to c#?

My below c# code is very simple.

string token = string.Empty;
string key = "9T5zhB4sTNGxMJ-iDdO-Ow" + "8rdp7erdig0m6aa72lhanvuk01" + "pizza1";
string message = "1387797294";
Encoding encoding = Encoding.UTF8;

//token 9036a1a3f654aefeab426e9f7e17288e
Debug.WriteLine(MD5.GetMd5String(message + key));
Asked By: LojiSmith

||

Answers:

First, in python, you can simply use the hmac module:

>>> import hmac
>>> hmac.new("9T5zhB4sTNGxMJ-iDdO-Ow"+"8rdp7erdig0m6aa72lhanvuk01"+"pizza1", "1387797294").hexdigest()
'9036a1a3f654aefeab426e9f7e17288e'

Second, your C# code does not implement the HMAC algorithm, but simply returns a MD5 hash.
The python equivalent would be simply

>>> import md5
>>> md5.new("1387797294" + "9T5zhB4sTNGxMJ-iDdO-Ow"+"8rdp7erdig0m6aa72lhanvuk01"+"pizza1").hexdigest()
'8280c9a3804b53792324b62363fc22fd'

You can find an implemention of the HMAC algorithm in C# e.g. at CodePlex.

From the project page:

Project Description
This is a simple implementation of the MD5
cryptographic hashing algorithm and HMAC-MD5. This class consists of
fully transparent C# code, suitable for use in .NET, Silverlight and
WP7 applications.


Also, here’s another simple implementation I’ve come up with:

string key = "9T5zhB4sTNGxMJ-iDdO-Ow" + "8rdp7erdig0m6aa72lhanvuk01" + "pizza1";
string message = "1387797294";

var encoding = Encoding.UTF8;
var md = System.Security.Cryptography.MD5CryptoServiceProvider.Create();
var trans_5C = new byte[64];
var trans_36 = new byte[64];

var b_key = encoding.GetBytes(key);
// TODO: also check if key is to short
if (b_key.Length > 64)
    b_key = md.ComputeHash(b_key);

for (int i = 0; i < 64; i++)
{
    trans_5C[i] = 92;
    trans_36[i] = 54;
    if (i < key.Length)
    {
        trans_5C[i] ^= b_key[i];
        trans_36[i] ^= b_key[i];
    }
}

byte[] inner = md.ComputeHash(trans_36.Concat(encoding.GetBytes(message)).ToArray());
var hash = md.ComputeHash(trans_5C.Concat(inner).ToArray());

StringBuilder sb = new StringBuilder();
foreach (byte b in hash)
    sb.Append(b.ToString("x2"));
var result = sb.ToString(); // = 9036a1a3f654aefeab426e9f7e17288e
Answered By: sloth