1

为什么我从我的函数中得到这个输出?

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

输出:

407a9d8868a678e12d9fc0264f9ae11e8761b557
0000000000000000000000000000000000000000000000000000000000000000

base_convert($var, 16, 10)输出

1421821959848150668406846884086820088622688484226正确。

另外,作为一个附带问题(对此有加分!)我假设ripemd160为我提供了每个输入原像的唯一标识符。我正在尝试制作一个 url-shortening 服务,将 URL 从任何长度缩短为其哈希摘要(我假设将二进制文件转换为 base64base64_encode($converted)将进一步缩短 URL)。这是正确的,这是一个好主意吗?

4

2 回答 2

4

base_convert上的 PHP 文档说

由于与使用的内部“double”或“float”类型相关的属性,base_convert() 可能会丢失大数的精度。有关更多具体信息和限制,请参阅手册中的浮点数部分。

因此,您不能依赖此函数来转换大量数字。但是,手动编写一个函数来从基数 16 转换为基数 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');

我假设使用 base64_encode($converted) 将二进制文件转换为 base64 会进一步缩短 URL)。这是正确的吗,这是个好主意吗

是的,它更短。它比二进制短 32 倍,比 base-16 短 4 倍。但是,ripemd160 不保证为每个链接提供唯一标识符。仍然有一些碰撞(我什至不知道它会多么罕见)。

于 2013-09-17T06:41:20.377 回答
4

根据 PHP 手册,精度base_convert()限制为32 位doublefloat32 位。您可以使用gmp库来处理任意长度的数字。

来自 PHP 手册页的示例代码:

/* 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 );
}
于 2013-09-17T06:56:01.967 回答