开发者

Convert SHA Hash Computation in Python to C#

开发者 https://www.devze.com 2023-01-19 15:08 出处:网络
Can someone please help me convert the following two lines of python to C#. hash = hmac.new(secret, data, digestmod = hashlib.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


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));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消