No_Name
No_Name

Reputation: 27

How To Convert a function value (bytes) to decimal values (0-255 each)

i'm implementing a ProvablyFair algorithm. One of the steps is to get the first four bytes of a hashed value as separate integers.

here is what I've attempted till here:

  <?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("%s\n", "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>";

but the next step is : The first four bytes of the HMAC_SHA256($K, $message) function’s result need to be converted into decimal values (0-255 each).

how to convert $byte1 , $byte2 , $byte3 , $byte4 to decimal values ?

Upvotes: 1

Views: 158

Answers (1)

lukas.j
lukas.j

Reputation: 7163

$byte1 , $byte2 , $byte3 , $byte4 are the decimal values.

You could also shorten this line:

$byte_array = array_values(unpack('C*', $goHash));

to

$byte_array = unpack('C4', $goHash);   // with 4 instead of * you get the first 4 bytes

Upvotes: 1

Related Questions