1

I am converting .net project to PHP . But unable to convert the following code:

public static string HashString(string value)
    {
        SHA1 hasher = new SHA1CryptoServiceProvider();

        UTF8Encoding enc = new UTF8Encoding();

        byte[] hashInBytes = hasher.ComputeHash(enc.GetBytes(value));

        return Convert.ToBase64String(hashInBytes);
    }

So far I have done this but result is not same:

function HashString($str) {
  return base64_encode(sha1($str));
}

Please help, thanks.

  • possible duplicate of [C# SHA-1 vs. PHP SHA-1...Different Results?](http://stackoverflow.com/questions/790232/c-sharp-sha-1-vs-php-sha-1-different-results) – Raptor Dec 19 '14 at 06:15

2 Answers2

1

The reason behind the difference is, PHP uses ASCII encoding for hash calculations.

In C#, you can replace UTF8Encoding with ASCIIEncoding in order to have same results.

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

Finally I found the solution:

This is the final code which is equivalent to .net code:

function HashString($str) {
  return base64_encode(sha1($str,true));
}

I have added "true" with sha1 function.