等价于C#中的PHP sha1,原始二进制格式,长度为20


equivalent PHP sha1 in C#, with raw binary format and a length of 20

可能重复:
C#SHA-1与PHP SHA-1…不同的结果?

我试图找出C#中的PHP sha1(string,true);等效方法是什么。我试着搜索了很多,也试着写了一些代码,但找不到任何解决方案。有人能告诉我如何获得长度为20的行二进制格式的sha1吗,就像PHP手册中的PHP函数:SHA1一样?

在PHP中:

sha1($sampletext, true);

在C#中:

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

两者都返回不同的输出。

感谢

这个IDEOne测试似乎产生了与在C#中对哈希使用UTF-8编码相同的输出:

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));
}

这将是特定于平台的,取决于PHP如何处理它。它甚至可能是一些文档中的ASCII。这一切都与字符编码有关。