我正在实现一个ProvablyFair算法。其中一个步骤是将散列值的前四个字节作为单独的整数。
这里是我尝试到这里的内容:
<?php
$K = "95ac4ed338c4b1e9ce4a0e2fb7bb400fa7f3ee5e62264432acb37e27619fe2ec";
$clientSeed = 98889;
$nonce = 21;
$row_number = 1;
$message = "{$clientSeed}:{$nonce}:{$row_number}:0";
$goHash = hash_hmac('sha256', $K, $message);
echo "Hash : " . $goHash;
echo "<br>";
echo "<br>";
printf("%sn", "Bin To Hex : " . bin2hex($goHash));
echo "<br>";
echo "<br>";
$byte_array = array_values(unpack('C*', $goHash));
echo "First 4 Bytes (Array) : ";
$Get_1St_4_Bytes = (array_slice($byte_array, 0, 4));
print_r($Get_1St_4_Bytes);
echo "<br>";
echo "<br>";
$byte1 = $Get_1St_4_Bytes[0];
$byte2 = $Get_1St_4_Bytes[1];
$byte3 = $Get_1St_4_Bytes[2];
$byte4 = $Get_1St_4_Bytes[3];
echo "First 4 Bytes : "."[$byte1] [$byte2] [$byte3] [$byte4]";
echo "<br>";
echo "<br>";
echo octdec($byte1);
echo "<br>";
,但下一步是:HMAC_SHA256($K, $message)函数结果的前四个字节需要转换为十进制值(每个0-255)。
如何将$byte1
,$byte2
,$byte3
,$byte4
转换为十进制值
$byte1, $byte2, $byte3, $byte4十进制值
你也可以缩短这一行:
$byte_array = array_values(unpack('C*', $goHash));
$byte_array = unpack('C4', $goHash); // with 4 instead of * you get the first 4 bytes