php:将包含 0..9a..f 的 varchar(32) 快速转换为 varchar ([log[38]16]+1)...0..9A..z

发布于 2024-12-18 07:52:15 字数 197 浏览 4 评论 0原文

假设我们有一个字符串 md5('somestring')。它将包含符号 0..f。因此,char(32) 可以保存该哈希值,但我相信它不会超过 21 个字节 ([log 38/ log 236 + 1])*Length(hash)。有什么快速函数可以将符号 0..f 的字符串转换为符号 0..9A..z 的字符串吗? (这将需要超过 21 个字节,因为它只使用数字和拉丁字母)?

Suppose we have a string md5('somestring'). It will contain symbols 0..f. So, char(32) is OK to save that hash, but I beleive it could take no more than 21 bytes ([log 38/ log 236 + 1])*Length(hash). Any fast function to convert string with symbols 0..f into a string with symbols 0..9A..z? (which will take more than 21 bytes, because it uses only numbers and latin letters)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-12-25 07:52:15

更好:以二进制形式散列结果。使用索引,二进制运行速度更快。

使用 mysql 创建一个字段 bin(16)。查询如下:

    SELECT * FROM `table` WHERE `field` = UNHEX('md5 hash')

从 PHP 使用此函数(十六进制到 bin)

    function convert($hexString)
    {
            $hexLenght = strlen($hexString);
            // only hex numbers is allowed
            if ($hexLenght % 2 != 0 || preg_match("/[^\da-fA-F]/",$hexString)) return FALSE;

            unset($binString);
            for ($x = 1; $x <= $hexLenght/2; $x++)
            {
                    $binString .= chr(hexdec(substr($hexString,2 * $x - 2,2)));
            }

            return $binString;
    } 

您还可以使用:
http://php.net/manual/en/function.hex2bin.php

http://php.net/manual/en/function.bin2hex.php

Better : hash the result in binary. Binary run faster and much more faster with indexes.

With mysql create a field bin(16). Query are like this :

    SELECT * FROM `table` WHERE `field` = UNHEX('md5 hash')

From PHP Use this function (hex to bin)

    function convert($hexString)
    {
            $hexLenght = strlen($hexString);
            // only hex numbers is allowed
            if ($hexLenght % 2 != 0 || preg_match("/[^\da-fA-F]/",$hexString)) return FALSE;

            unset($binString);
            for ($x = 1; $x <= $hexLenght/2; $x++)
            {
                    $binString .= chr(hexdec(substr($hexString,2 * $x - 2,2)));
            }

            return $binString;
    } 

You can also use :
http://php.net/manual/en/function.hex2bin.php

http://php.net/manual/en/function.bin2hex.php

╰◇生如夏花灿烂 2024-12-25 07:52:15

第二个参数设置为true:

string md5 ( string $str [, bool $raw_output = false ] )

Set the second parameter to true:

string md5 ( string $str [, bool $raw_output = false ] )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文