Difference between PHP and PHP 64 Bits

3

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

I was researching on RealPerson.js , where I had one code for PHP and another for PHP 64 Bit:

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
    
asked by anonymous 18.10.2016 / 00:18

1 answer

8

Basically, because one has not taken care to abstract the numeric formats when they were made, the PHP functions have different numerical capabilities in each version.

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

In the code in question, especially 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 were 64-bit, 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 functionality of the << operator, which is the leftShift32($number, $steps) function.

By the way, it has been redone somewhat "strange", because instead of using bit operations, it uses several conversions and string operations.

It would have been better to make a version that only works well in both cases, and using math more appropriately than depending on the user to evaluate which one to use in what situation.

    
18.10.2016 / 00:31