2009-03-30 15 views
8

यह मेरा PHP कोड है:hmac_sha256 php और सी # में मतभेद है

hash_hmac("sha256", utf8_encode($filename), utf8_encode($password)); 

और यह मेरा सी # कोड है:

var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(password)); 
hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(filename)); 

दुर्भाग्य से दोनों परिणाम भिन्न होते हैं। क्या कोई मुझे संकेत दे सकता है?

उत्तर

16

मेरा सी # सबसे अच्छा नहीं है लेकिन मुझे यह काम करने के लिए मिला है, आपको अपने बाइट सरणी परिणामों को हेक्स में परिवर्तित करना है।

पीएचपी

$hash = hash_hmac("sha256", utf8_encode("Filename"), utf8_encode("Password")); 
echo $hash; 
// 5fe2ae06ff9828b33fe304545289a3f590bfd948ca9ab731c980379992ef41f1 

सी #

string password = "Password"; 
string filename = "Filename"; 

var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(password)); 
hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(filename)); 

foreach(byte test in hmacsha256.Hash) 
{ 
    Console.Write(test.ToString("X2")); 
} 
// 5FE2AE06FF9828B33FE304545289A3F590BFD948CA9AB731C980379992EF41F1 
+0

धन्यवाद - मैं दोगुना होगा की जाँच करें और इस शाम वापस तुम्हारे पास आया। मैंने पहले से ही बिटकॉन्टर.ओस्ट्रिंग (hmacsha256.Hash) का उपयोग किया है, लेकिन मुझे गलत परिणाम मिल गए हैं। – tanascius

+0

आपका स्वागत है :) –

संबंधित मुद्दे