doudouxuqh198138 2010-06-30 15:28
浏览 47
已采纳

将C哈希函数转换为PHP:ezmlm_hash

I'm trying to upgrade from PHP 5.2.x to 5.3.2 on my server. Problem is, I relying on the broken implementation of PHP's ezmlm_hash() (the bug is outlined here: http://bugs.php.net/bug.php?id=47969).

My first thought was to rewrite the broken version of the native PHP function (which is written in C) myself in PHP and use that in my code, instead of modifying the PHP source code and having to compile PHP from source.

Here is the C version of the code:

PHP_FUNCTION(ezmlm_hash)
{
    char *str = NULL;
    unsigned int h = 5381L;
    int j, str_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
                              &str, &str_len) == FAILURE) {
        return;
    }

    for (j = 0; j < str_len; j++) {
        h = (h + (h << 5)) ^ (unsigned long) (unsigned char) tolower(str[j]);
    }

    h = (h % 53);

    RETURN_LONG((int) h);
}

here is what I've written in PHP:

function ezmlm_hash_mine($email_address){
    $h = 5381;
    $email_length = strlen($email_address);
    for($x=0;$x<$email_length;$x++){
        $chr = strtolower($email_address[$x]);
        $h = ($h + ($h << 5)) ^ ( ord($chr) );
    }

    $h = $h % 53;
    return $h;
}

I'm using a 64-bit machine. The two functions output different results:

$email_addresses = array(
    'test@example.com',
    'mike@example.com',
);

print('<PRE>');

foreach($email_addresses as $email_address){
    print(ezmlm_hash($email_address).PHP_EOL);
    print(ezmlm_hash_mine($email_address).PHP_EOL.PHP_EOL);
}

output:

23
-52

15
-21

I know I probably have some precision or typing issues, I'm just not sure how to fix it. Any help would be greatly appreciated!

UPDATE

When I run thes the code on 32 bit machines, they both output the new corrected values:

12
12

45
45

I think this has something to do with the modulo operator... does anyone know the PHP equivalent of the C modulo operator? % in PHP behaves differently!

UPDATE 2

It appears as if this is not possible with vanilla PHP, as it's floating point arithmetic doesn't have enough precision, and weirdness in . I'll have to install either BCMath or GMP. Thanks for everyone's insight.

  • 写回答

2条回答 默认 最新

  • dongpingwu8378 2010-06-30 16:09
    关注

    try this EDIT truncate to 32 bits after calculation:

    function ezmlm_hash_mine($email_address){
        $h = gmp_init(5381);
        $d = gmp_setbit(0, 64);
        $d32 = gmp_setbit(0, 32);
        $email_length = strlen($email_address);
    
        $chr = strtolower($email_address);
    
        for($x=0;$x<$email_length;$x++){    
            $h = gmp_mod(gmp_xor(gmp_mod(gmp_add($h, gmp_mod(gmp_mul($h, "32"), $d)), $d), ord($chr[$x])), $d32);
        }
    
        $h = gmp_mod($h, 53);
        return gmp_intval($h);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值