将C#HMAC SHA256转换为PHP

时间:2016-03-16 08:43:01

标签: c# php sha256 hmac

我需要转换为PHP的C#方法。我尝试了几件事,但我仍然得到了不同的结果。不幸的是我无法改变C#应用程序的任何内容。它必须是现实的。也许你们中的一个可以帮忙吗?

C#:

static public void Main ()
{
    string StringToSign = "test";
    string Key = "123456";

    //Calculate Signature
    string Signature = CalculateSignature(StringToSign, Key);

    Console.WriteLine ("StringToSign: " + StringToSign);
    Console.WriteLine ("Key: " + Key);
    Console.WriteLine ("Signature Caculated: " + Signature + "\r\n");
}

static private string CalculateSignature(String StringToSign, String Key)
{
    Encoding enc = Encoding.GetEncoding(65001);

    byte[] KeyHex = StringHexValuesToByteArray(Key);
    byte[] StringToSign_byte = enc.GetBytes(StringToSign);

    //Check Signature
    HMACSHA256 hmac = new HMACSHA256(KeyHex);
    byte[] hashValue = hmac.ComputeHash(StringToSign_byte);

    return BitConverter.ToString(hashValue).Replace("-", "");
}

static public byte[] StringHexValuesToByteArray(string str)
{
    if (str.Length % 2 != 0)
        return null;

    string s = string.Empty;
    byte[] ret = new byte[str.Length / 2];

    for (int run = 0; run < str.Length / 2; run++)
    {
        s = str.Substring(run * 2, 2);
        ret[run] = Convert.ToByte(s, 16);
    }
    return ret;
}

PHP:

public function send() {

    $stringToSign = 'test';
    $key = '123456';

    //Calculate Signature
    $signature = $this->calculateSignature($stringToSign, $key);

    print_r("StringToSign: " . $stringToSign . PHP_EOL);
    print_r("Key: " . $key . PHP_EOL);
    print_r("Signature Caculated: " . $signature . PHP_EOL);
}

private function calculateSignature($stringToSign, $key) {

    // check signature
    $hash = strtoupper(hash_hmac('sha256', $stringToSign, $key, false));
    return $hash;
}

为了更好地理解,以下是上面代码块的输出:

C#

  

StringToSign:test

     

密钥:123456

     

Signature Caculated:   DA3617974490FB780F04F06287BF93B0F24A7F15970471146428B943FFDC7850

PHP

  

StringToSign:test

     

GroupKey:123456

     

签名计算:9D2BB116E1DF997FFE8A5139FC1D187F976C19579A138414A112BC2E39020EBA

1 个答案:

答案 0 :(得分:1)

如果您想修改PHP以使其等同于C#

更改

$hash = strtoupper(hash_hmac('sha256', $stringToSign, $key, false));

$hash = strtoupper(hash_hmac('sha256', $stringToSign, hex2bin($key), false));

请务必检查代码是否适用于{_ 1}}等非ASCII字符。

ideone here

如果您想修改C#代码以使其等同于PHP

你使一切变得复杂:

àèéìòù

没有任何十六进制。而不是使用static private string CalculateSignature(String stringToSign, String key) { byte[] key_byte = Encoding.UTF8.GetBytes(key); byte[] stringToSign_byte = Encoding.UTF8.GetBytes(stringToSign); //Check Signature HMACSHA256 hmac = new HMACSHA256(key_byte); byte[] hashValue = hmac.ComputeHash(stringToSign_byte); return BitConverter.ToString(hashValue).Replace("-", ""); } 通常最好使用Encoding.GetEncoding(65001),因为每个人都更清楚。

请注意,编码可能存在问题。尝试计算Encoding.UTF8之类的hmac,看看PHP正在使用哪种编码。