Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SHA Hash Computation in Python to C#

Tags:

python

c#

hash

sha1

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

like image 536
Sara Avatar asked Oct 10 '10 08:10

Sara


People also ask

How does Python calculate SHA256 hash of a file?

SHA256 can be calculated to text data easily by using the hashlib module sha256() method. The text data is provided as a parameter to the sha356() method. This method generates a result where the hexdigest() method of the result can be used to print the SHA265 in hexadecimal format.

What does Hashlib do in Python?

This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA's MD5 algorithm (defined in internet RFC 1321).


1 Answers

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));
    }
}
like image 160
Darin Dimitrov Avatar answered Sep 23 '22 16:09

Darin Dimitrov