2

Possible Duplicate:
C# SHA-1 vs. PHP SHA-1…Different Results?

I am trying to find out what the PHP sha1(string,true); equivalent method is in C#. I tried searching a lot and also tried writing some code, but couldn't find any solution. Can anyone tell me how to get sha1 in row binary format with a length of 20, like the PHP function: SHA1 in the PHP manual?

In PHP:

sha1($sampletext, true);

In C#:

string sampletext= "text";
SHA1 hash = SHA1CryptoServiceProvider.Create();
byte[] plainTextBytes = Encoding.ASCII.GetBytes(dataString);
byte[] hashBytes = hash.ComputeHash(plainTextBytes);

Both return a different output.

Thanks

Community
  • 1
  • 1
Don Yogesh
  • 35
  • 2
  • 6

1 Answers1

2

This IDEOne test seems to produce the same output as using UTF-8 encoding for the hash in C#:

string sampletext = "text";
System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1CryptoServiceProvider.Create();
byte[] plainTextBytes = Encoding.UTF8.GetBytes(sampletext);
byte[] hashBytes = hash.ComputeHash(plainTextBytes);

foreach (byte b in hashBytes) {
    Console.Write(string.Format("{0:x2}", b));
}

This will be platform-specific depending on how the PHP handles it. It may even be ASCII from some of the documentation. It's all to do with character encoding.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138