1
Reply

What is the Equivalent c# method for the php "hash_hmac()"?

Girija Pugazhenthi

Girija Pugazhenthi

Jan 19 2018 2:16 AM
105
I have a sample code in php.I need to implement this in c#.I tried the following but I am getting different values in both.
 
Please suggest me an exact hash method in c#.
 
php code: 
  1. $signature = $this->hexToBase64(hash_hmac("sha1"$string_to_sign, self::SECRET_KEY));  
  2.    
  3. private function hexToBase64($hex){  
  4. $return = "";  
  5. foreach(str_split($hex, 2) as $pair){  
  6. $return .= chr(hexdec($pair));  
  7. }  
  8. return base64_encode($return);  
  9. }  
signature: ZToYIGWks4dBLImEKWozVZTkxZc= 
 
c# code: 
  1. var key = Encoding.UTF8.GetBytes(apiKey);  
  2. string signature ;  
  3. using (var hmac = new HMACSHA1(key))  
  4. {  
  5. var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));  
  6. signature = Convert.ToBase64String(hash);  
  7. }  
signature: wR/E2jbMo9Y87aqXllcZAynCyRE=

Answers (1)