2010-10-10 9 views
4

Quelqu'un peut-il m'aider s'il vous plaît convertir les deux lignes suivantes de python en C#.Convertir SHA Hash Computation en Python en C#

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

Le reste ressemble à ceci si vous êtes 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 

Merci

Répondre

6

Vous pouvez utiliser la classe HMACSHA1 pour calculer le hachage:

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

Je vous remercie beaucoup! – Sara

+0

Et pour imprimer la clé, j'ai ajouté: string key = string.Empty; foreach (octet b dans le hachage) { key + = b.ToString ("X2"); } MessageBox.Show (touche); – Sara

+4

Ou vous pourriez 'BitConverter.ToString (hash) .Replace (" - ", string.Empty)'. –