Difference between PHP and PHP 64 Bits

Asked

Viewed 143 times

3

I would like to know the difference between PHP and PHP 64 Bits.

I was researching about the RealPerson.js, where there was code for PHP and another for PHP 64 Bits:

PHP

function rpHash($value) { 
    $hash = 5381; 
    $value = strtoupper($value); 
    for($i = 0; $i < strlen($value); $i++) { 
        $hash = (($hash << 5) + $hash) + ord(substr($value, $i)); 
    } 
    return $hash;
} 
if (rpHash($_POST['realPerson'].salt) == $_POST['realPersonHash']) { 
    // Accepted

PHP 64bit

function rpHash($value) { 
    $hash = 5381; 
    $value = strtoupper($value); 
    for($i = 0; $i < strlen($value); $i++) { 
        $hash = (leftShift32($hash, 5) + $hash) + ord(substr($value, $i)); 
    } 
    return $hash; 
} 

// Perform a 32bit left shift 
function leftShift32($number, $steps) { 
    // convert to binary (string) 
    $binary = decbin($number); 
    // left-pad with 0's if necessary 
    $binary = str_pad($binary, 32, "0", STR_PAD_LEFT); 
    // left shift manually 
    $binary = $binary.str_repeat("0", $steps); 
    // get the last 32 bits 
    $binary = substr($binary, strlen($binary) - 32); 
    // if it's a positive number return it 
    // otherwise return the 2's complement 
    return ($binary{0} == "0" ? bindec($binary) : 
            -(pow(2, 31) - bindec(substr($binary, 1)))); 
} 

if (rpHash($_POST['realPerson'].salt) == $_POST['realPersonHash']) { 
    // Accepted

1 answer

8


Basically, because it was not taken care to abstract the numerical formats when they were elaborated, the PHP functions have different numerical capabilities in each version.

Thus, in some situations it is necessary to use proper functions in place of native ones for the behavior to be equal in both.

In the code in question, in particular in this part:

$hash << 5

This works this way by shifting the value 5 bits to the left:

11000000 00001000 00000011 00000000
<< 5 =
00000001 00000000 01100000 00000000

If it was in 64 bits, this would happen:

00000000 00000000 00000000 00000000 11000000 00001000 00000011 00000000
<< 5 =
00000000 00000000 00000000 00011000 00000001 00000000 01100000 00000000
                               ^
                               isto aqui não aconteceria em 32
                               bits, o valor seria truncado, o
                               que geraria resultados diferentes
                               nas duas arquiteturas.

Therefore, it was necessary to rewrite the operator’s functionality <<, which is the function leftShift32($number, $steps).

By the way, it was redone in a "strange" way, because instead of using bit operations, it uses several conversions and string operations.

It would have been better to make a single version that worked well in both cases, and using mathematics in a more appropriate way, than to depend on the user to evaluate which to use in which situation.

  • 1

    Great explanation, I understand perfectly! Thank you for clarifying this for me!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.