Why am I getting this output from my function?
echo $var = hash_hmac('ripemd160', 'http://www.weburlhere.org', 0, 0); echo "rn"; echo $converted = base_convert($var, 16, 2); echo "rn";
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?
Advertisement
Answer
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).