В вашем приложении C # вы генерируете массивы byte [] двумя разными способами с немного отличающимися результатами. Ваш PHP-скрипт должен точно имитировать их.
hash.Key = HexToByte (encryptionKey)
Вы передаете строку длиной 16 символов и получаете массив из 8 байтов, как
hash.Key = new byte[]{0xAE, 0x09, 0xF7, 0x2B, 0x00, 0x7C, 0xAA, 0xB5 };
, но
<code>string password = "pa55w0rd";
byte[] b = Encoding.Unicode.GetBytes(password)
возвращает массив из 16 элементов из-за кодировка. Юникод, например
byte[] b = { 0x112, 0x0, 0x97, 0x0, 0x53, 0x0, 0x53, 0x0, 0x119, 0x0, 0x48, 0x0, 0x114, 0x0,0x100, 0x0 }
В вашем php-скрипте вы можете изменить кодировку $ password на utf-16le с помощью
$ data = mb_convert_encoding ($ password, 'UTF16-LE') для достижения аналогичного результата. hash_hmac (), не знающий о какой-либо кодировке, будет обрабатывать строку как 16-байтовую однобайтовую закодированную строку, так же, как hash.ComputeHash (byte []) в .net.
<code><?php
$password = "pa55w0rd";
$key = HexToBytes("AE09F72B007CAAB5"); // 8 bytes, hex</p>
<p>// $to must be 'UTF-16LE'
// $from depends on the "source" of $password
$data = mb_convert_encoding($password, 'UTF-16LE', 'ASCII');</p>
<p>// I've saved this script as an ascii file -> the string literal is ASCII encoded
// therefore php's strlen() returns 8 for $password and 16 for $data
// this may differ in your case, e.g. if the contents of $password comes from a
// http-request where the data is utf-8 encoded. Adjust the $from parameter for
// mb_convert_encoding() accordingly
echo 'Debug: |data|=', strlen($data), ' |password|=', strlen($password), "\n"; </p>
<p>$h = HexToBytes(hash_hmac('sha1', $data, $key));
echo 'hmac-sha1: ', base64_encode($h);</p>
<p>function HexToBytes($s) {
// there has to be a more elegant way...
return join('', array_map('chr', array_map('hexdec', str_split($s, 2))));
}
печать
Debug: |data|=16 |password|=8
hmac-sha1: oK9NOVhpTkxLoLfvh1430SFb5gw=