0

我知道这是一个问题,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
4

1 回答 1

0

我认为你在 c# 中得到原始字节,而在 php sha1() 中默认返回十六进制。您可以将 true 作为第二个参数传递给 php 的 sha1() 以获取原始字节。或者在 c# 中转换为十六进制。

于 2013-11-12T11:07:20.093 回答