我知道这是一个问题,c# 和 php 编码 sha1 哈希的方式不同,但我一直无法找到 ac# sha1 的 php 版本的示例。
c#代码:
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