Convert SHA Hash Computation in Python to C#

左心房为你撑大大i 提交于 2019-12-06 00:59:59

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/3899644/convert-sha-hash-computation-in-python-to-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!