我知道这是一个问题,其中c#和php编码sha1哈希的方式不同,但我一直无法找到c#sha1的php版本的例子。
c#code:
private string GetHashedKey(string supplierPrivateKey, DateTime now)
{
if (string.IsNullOrEmpty(supplierPrivateKey))
return "";
supplierPrivateKey += now.ToString("yyyy/MM/dd HH:mm:ss");
Encoding enc = Encoding.UTF8;
byte[] buffer = enc.GetBytes(supplierPrivateKey);
SHA1CryptoServiceProvider cryptoTransformSha1 =
new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(
cryptoTransformSha1.ComputeHash(buffer)).Replace("-", "");
return hash;
}
php:
$hashedSupplierPrivateKey = $supplierPrivateKey.gmdate("Y/m/d H:i:s");
$hashedSupplierPrivateKey = utf8_encode($hashedSupplierPrivateKey);
$hashedSupplierPrivateKey = sha1($hashedSupplierPrivateKey,true);
//Error here
$hashedSupplierPrivateKey = str_replace("-", "", $hashedSupplierPrivateKey);
$hashedSupplierPrivateKey = strtoupper($hashedSupplierPrivateKey);
echo $hashedSupplierPrivateKey;
以下是在c#
中生成的正确哈希的示例 530DFA9CD08CF36017B7C781E1A8D0CEC74CB944
答案 0 :(得分:0)
我认为你在c#中得到原始字节,而在php sha1()中默认返回十六进制。您可以将true作为第二个参数传递给php的sha1()以获取原始字节。或者在c#中转换为十六进制。