C# sha1加密

最近在廠商對接API的時後才知道,

sha1加密在C#和PHP的結果是不一樣的,

C#的寫法如下:

 SHA1 sha1 = new SHA1CryptoServiceProvider();
 byte[] source = Encoding.Default.GetBytes(input);
 byte[] crypto = sha1.ComputeHash(source);
 string result = Convert.ToBase64String(crypto);Byte[]轉為字串
 return result;

可以透過底下網址來測試PHP Sha1的加密結果

https://www.movable-type.co.uk/scripts/sha1.html

C#為了配合PHP的寫法,需修改寫法如下:

byte[] temp1 = Encoding.UTF8.GetBytes(data);
 SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
 byte[] temp2 = sha.ComputeHash(temp1);
 sha.Clear(); 
 var output = BitConverter.ToString(temp2);
 output = output.Replace("-", "");
 output = output.ToLower();
 return output;