C# SHA-1 vs. PHP SHA-1...Different Results? C# SHA-1 vs. PHP SHA-1...Different Results? php php

C# SHA-1 vs. PHP SHA-1...Different Results?


Use ASCIIEncoding instead of UnicodeEncoding. PHP uses ASCII charset for hash calculations.


This method in .NET is equivalent to sha1 in php:

string sha1Hash(string password){    return string.Join("", SHA1CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(password)).Select(x => x.ToString("x2")));}


I had this problem also. The following code will work.

string dataString = "string to hash";SHA1 hash = SHA1CryptoServiceProvider.Create();byte[] plainTextBytes = Encoding.ASCII.GetBytes(dataString);byte[] hashBytes = hash.ComputeHash(plainTextBytes);string localChecksum = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();