tags:

views:

48

answers:

1

Can someone please help me convert the following two lines of python to C#.

hash = hmac.new(secret, data, digestmod = hashlib.sha1)
key = hash.hexdigest()[:8]

The rest looks like this if you're intersted:

#!/usr/bin/env python

import hmac
import hashlib


secret = 'mySecret'     
data = 'myData'

hash = hmac.new(secret, data, digestmod = hashlib.sha1)
key = hash.hexdigest()[:8]

print key

Thanks

+2  A: 

You could use the HMACSHA1 class to compute the hash:

class Program
{
    static void Main()
    {
        var secret = "secret";
        var data = "data";
        var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(secret));
        var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
        Console.WriteLine(BitConverter.ToString(hash));
    }
}
Darin Dimitrov
Thank you very much!
Sara
And to print the key, I added: string key = string.Empty; foreach (byte b in hash) { key += b.ToString("X2"); } MessageBox.Show(key);
Sara
Or you could `BitConverter.ToString(hash).Replace("-", string.Empty)`.
Darin Dimitrov