Вопрос

Why am I getting this output from my function?

echo $var = hash_hmac('ripemd160', 'http://www.weburlhere.org', 0, 0);
echo "\r\n";
echo $converted = base_convert($var, 16, 2);
echo "\r\n";

Outputs:

407a9d8868a678e12d9fc0264f9ae11e8761b557
0000000000000000000000000000000000000000000000000000000000000000

Whereas base_convert($var, 16, 10) outputs

1421821959848150668406846884086820088622688484226 correctly.

Also, as a side-question (bonus points for this!) I'm assuming ripemd160 gives me a unique identifier for each input preimage. I'm attempting to make a url-shortening service that shortens a URL from any length to its hash digest (I'm assuming converting the binary to base64 with base64_encode($converted) will shorten the URL even more). Is this correct, and is this a good idea?

Это было полезно?

Решение

The PHP document on base_convert said

base_convert() may lose precision on large numbers due to properties related to the internal "double" or "float" type used. Please see the Floating point numbers section in the manual for more specific information and limitations.

So, you cannot rely on this function to convert a large numbers. However, it is very easy manually write a function to convert from base 16 to base 2.

function hex2bin($hex) {
    $table = array('0000', '0001', '0010', '0011', 
                   '0100', '0101', '0110', '0111',
                   '1000', '1001', 'a' => '1010', 'b' => '1011', 
                   'c' => '1100', 'd' => '1101', 'e' => '1110', 
                   'f' => '1111');
    $bin = '';
    
    for($i = 0; $i < strlen($hex); $i++) {
        $bin .= $table[strtolower(substr($hex, $i, 1))];
    }
    
    return $bin;
}
echo hex2bin('407a9d8868a678e12d9fc0264f9ae11e8761b557');

I'm assuming converting the binary to base64 with base64_encode($converted) will shorten the URL even more). Is this correct, and is this a good idea

Yes, it is shorter. It is 32 times shorter than binary, and 4 times shorter than base-16. However, ripemd160 does not guarantee to give an unique identifier for every link. There are still some collisions (which I don't even know how rare it will be).

Другие советы

According to the PHP manual, the base_convert() is limited to double or float 32-bit precision. You can use gmp library to deal with numbers of arbitrary length.

A sample code also from the PHP manual page:

/* use gmp library to convert base. gmp will convert numbers > 32bit
 * @author lindsay at bitleap dot com
 * @link you can execute this code at http://ideone.com/FT29qo
 */
function gmp_convert($num, $base_a, $base_b)
{
    return gmp_strval ( gmp_init($num, $base_a), $base_b );
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top