加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 哪些迭代规则适用于使用CRYPT_EXT_DES的crypt()?

发布时间:2020-12-13 22:48:04 所属栏目:PHP教程 来源:网络整理
导读:我的测试用例如下: echo crypt('string','_....salt');//errorecho crypt('string','_A...salt');//fastecho crypt('string','_AAAAsalt');//slow http://www.php.net/manual/en/function.crypt.php所述的解释: CRYPT_EXT_DES – Extended DES-based hash.
我的测试用例如下:

echo crypt('string','_....salt');//error
echo crypt('string','_A...salt');//fast
echo crypt('string','_AAAAsalt');//slow

http://www.php.net/manual/en/function.crypt.php所述的解释:

CRYPT_EXT_DES – Extended DES-based hash. The “salt” is a 9-character
string consisting of an underscore followed by 4 bytes of iteration
count and 4 bytes of salt. These are encoded as printable characters,
6 bits per character,least significant character first. The values 0
to 63 are encoded as “./0-9A-Za-z”. Using invalid characters in the
salt will cause crypt() to fail.

点是可打印的字符,为什么它会返回错误?哪个“订单”适用于使用的字符,导致“AAAA”迭代次数多于“A …”?

解决方法

这个问题有点陈旧,但是当我试图围绕如何创建一个内部使用的哈希类时,我发现了这个问题,我想出了这个基本功能,它将base64编码一个具有相应字符/意义的整数用作4个字符的’迭代计数’.可能的值为1到16,777,215

private function base64_int_encode($num){
    $alphabet_raw = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $alphabet = str_split($alphabet_raw);
    $arr = array();
    $base = sizeof($alphabet);
    while($num){
        $rem = $num % $base;
        $num = (int)($num / $base);
        $arr[]=$alphabet[$rem];
    }

    $arr = array_reverse($arr);
    $string = implode($arr);

    return str_pad($string,4,'.',STR_PAD_LEFT);
}

希望它可以帮到某人!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读