像URL缩短网站一样的PHP短哈希

cla*_*amp 78 php hash

我正在寻找一个PHP函数,它创建一个字符串或文件的短哈希,类似于像tinyurl.com这样的URL缩短网站

哈希值不应超过8个字符.

Rob*_*t K 152

TinyURL不会散列任何东西,它使用Base 36整数(甚至是62,使用小写和大写字母)来指示要访问的记录.

基数36到整数:

intval($str, 36);
Run Code Online (Sandbox Code Playgroud)

基数为36的整数:

base_convert($val, 10, 36);
Run Code Online (Sandbox Code Playgroud)

那么,而不是重定向到像/url/1234这样的路线/url/ax.这比散列更多地使用,因为不会发生冲突.通过这种方式,您可以轻松检查是否存在URL并在基础36中返回正确的现有ID,而无需用户知道它已在数据库中.

不要哈希,使用其他基础来做这种事情.(速度更快,可以防碰撞.)

  • 好一点,它应该是防撞的=) (2认同)

Kev*_*sJr 78

我写了一个小lib来从整数生成混淆的哈希.

http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash

$ids = range(1,10);
foreach($ids as $id) {
  echo PseudoCrypt::unhash($id) . "\n";
}
Run Code Online (Sandbox Code Playgroud)
m8z2p
8hy5e
uqx83
gzwas
38vdh
phug6
bqtiv
xzslk
k8ro9
6hqqy

2015年7月14日:添加下面的实际代码,因为它很难找到:

<?php
/**
 * PseudoCrypt by KevBurns (http://blog.kevburnsjr.com/php-unique-hash)
 * Reference/source: http://stackoverflow.com/a/1464155/933782
 * 
 * I want a short alphanumeric hash that’s unique and who’s sequence is difficult to deduce. 
 * I could run it out to md5 and trim the first n chars but that’s not going to be very unique. 
 * Storing a truncated checksum in a unique field means that the frequency of collisions will increase 
 * geometrically as the number of unique keys for a base 62 encoded integer approaches 62^n. 
 * I’d rather do it right than code myself a timebomb. So I came up with this.
 * 
 * Sample Code:
 * 
 * echo "<pre>";
 * foreach(range(1, 10) as $n) {
 *     echo $n." - ";
 *     $hash = PseudoCrypt::hash($n, 6);
 *     echo $hash." - ";
 *     echo PseudoCrypt::unhash($hash)."<br/>";
 * }
 * 
 * Sample Results:
 * 1 - cJinsP - 1
 * 2 - EdRbko - 2
 * 3 - qxAPdD - 3
 * 4 - TGtDVc - 4
 * 5 - 5ac1O1 - 5
 * 6 - huKpGQ - 6
 * 7 - KE3d8p - 7
 * 8 - wXmR1E - 8
 * 9 - YrVEtd - 9
 * 10 - BBE2m2 - 10
 */

class PseudoCrypt {

    /* Key: Next prime greater than 62 ^ n / 1.618033988749894848 */
    /* Value: modular multiplicative inverse */
    private static $golden_primes = array(
        '1'                  => '1',
        '41'                 => '59',
        '2377'               => '1677',
        '147299'             => '187507',
        '9132313'            => '5952585',
        '566201239'          => '643566407',
        '35104476161'        => '22071637057',
        '2176477521929'      => '294289236153',
        '134941606358731'    => '88879354792675',
        '8366379594239857'   => '7275288500431249',
        '518715534842869223' => '280042546585394647'
    );

    /* Ascii :                    0  9,         A  Z,         a  z     */
    /* $chars = array_merge(range(48,57), range(65,90), range(97,122)) */
    private static $chars62 = array(
        0=>48,1=>49,2=>50,3=>51,4=>52,5=>53,6=>54,7=>55,8=>56,9=>57,10=>65,
        11=>66,12=>67,13=>68,14=>69,15=>70,16=>71,17=>72,18=>73,19=>74,20=>75,
        21=>76,22=>77,23=>78,24=>79,25=>80,26=>81,27=>82,28=>83,29=>84,30=>85,
        31=>86,32=>87,33=>88,34=>89,35=>90,36=>97,37=>98,38=>99,39=>100,40=>101,
        41=>102,42=>103,43=>104,44=>105,45=>106,46=>107,47=>108,48=>109,49=>110,
        50=>111,51=>112,52=>113,53=>114,54=>115,55=>116,56=>117,57=>118,58=>119,
        59=>120,60=>121,61=>122
    );

    public static function base62($int) {
        $key = "";
        while(bccomp($int, 0) > 0) {
            $mod = bcmod($int, 62);
            $key .= chr(self::$chars62[$mod]);
            $int = bcdiv($int, 62);
        }
        return strrev($key);
    }

    public static function hash($num, $len = 5) {
        $ceil = bcpow(62, $len);
        $primes = array_keys(self::$golden_primes);
        $prime = $primes[$len];
        $dec = bcmod(bcmul($num, $prime), $ceil);
        $hash = self::base62($dec);
        return str_pad($hash, $len, "0", STR_PAD_LEFT);
    }

    public static function unbase62($key) {
        $int = 0;
        foreach(str_split(strrev($key)) as $i => $char) {
            $dec = array_search(ord($char), self::$chars62);
            $int = bcadd(bcmul($dec, bcpow(62, $i)), $int);
        }
        return $int;
    }

    public static function unhash($hash) {
        $len = strlen($hash);
        $ceil = bcpow(62, $len);
        $mmiprimes = array_values(self::$golden_primes);
        $mmi = $mmiprimes[$len];
        $num = self::unbase62($hash);
        $dec = bcmod(bcmul($num, $mmi), $ceil);
        return $dec;
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 这有一个非常聪明的设计= D golden primes = world.rock() (11认同)
  • 你应该让你的网站备份,或在https://github.com/KevBurnsJr/pseudocrypt下发布这个php版本 - 这是一个很棒的小lib!不想使用像YOURLS或PHURL这样的巨型"系统",只是一个很好的lib来创建短链接,就是这样.谢谢 (4认同)
  • 在一些评论者的帮助下,该帖子已更新为使用 bcmath,因此它现在应该是可靠的。有人还找到了一种使其可逆的方法,这完全是愚蠢的。 (3认同)
  • 我知道我正在评论一篇较旧的帖子.我想我会提到KevBurnsJr代码确实运行良好.但是,我最近刚刚从Windows 2003 32位服务器切换到Windows 2008 R2 x64服​​务器,我发现我正在复制唯一的哈希值.我现在必须找到另一种创建确认码的方法. (2认同)
  • http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash看起来网站已关闭,所以这里是该链接的副本;) (2认同)

Gum*_*mbo 44

URL缩短服务而是使用自动递增的整数值(如补充数据库ID),并使用Base64或其他编码对其进行编码,以便每个字符具有更多信息(64而不是仅仅10个相似的数字).

  • 如果您使用 base64,那么没有什么可以阻止脚本说 for($i=0;$i&lt;999999;$i++) { $pageContent=fread(fopen('http://www.yoururl.com/'.base64_encode( $i)); } 现在我可以访问您数据库中的每个 URL。 (3认同)
  • 这意味着什么(每个角色的信息更多)只是好奇!! (2认同)
  • @ravisoni 如果您使用十进制数字“0”–“9”来表示一个数字,则每个编码字符有 10 个可能的值(ld(10) ≈ 3.32 位/字符)。但是,如果您用 Base64 字符表示相同的数字,则每个编码字符有 64 个可能的值(ld(64) = 6 位/字符)。因此,使用 Base64,每个编码字符中存储了更多信息,即 6 位信息而不是 3.32 位。 (2认同)

Naz*_*riy 17

最短的哈希值是32个字符长度,你怎么能使用md5哈希的前8个字符

echo substr(md5('http://www.google.com'), 0, 8);
Run Code Online (Sandbox Code Playgroud)

更新:这里有另一个类在这里写的特拉维尔帕金斯这需要记录号码并为它创建短哈希.14位数字产生8位数字符串.到达这个数字的那一天,你变得比tinyurl更受欢迎;)

class BaseIntEncoder {

    //const $codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //readable character set excluded (0,O,1,l)
    const codeset = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

    static function encode($n){
        $base = strlen(self::codeset);
        $converted = '';

        while ($n > 0) {
            $converted = substr(self::codeset, bcmod($n,$base), 1) . $converted;
            $n = self::bcFloor(bcdiv($n, $base));
        }

        return $converted ;
    }

    static function decode($code){
        $base = strlen(self::codeset);
        $c = '0';
        for ($i = strlen($code); $i; $i--) {
            $c = bcadd($c,bcmul(strpos(self::codeset, substr($code, (-1 * ( $i - strlen($code) )),1))
                    ,bcpow($base,$i-1)));
        }

        return bcmul($c, 1, 0);
    }

    static private function bcFloor($x)
    {
        return bcmul($x, '1', 0);
    }

    static private function bcCeil($x)
    {
        $floor = bcFloor($x);
        return bcadd($floor, ceil(bcsub($x, $floor)));
    }

    static private function bcRound($x)
    {
        $floor = bcFloor($x);
        return bcadd($floor, round(bcsub($x, $floor)));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是如何使用它的示例:

BaseIntEncoder::encode('1122344523');//result:3IcjVE
BaseIntEncoder::decode('3IcjVE');//result:1122344523
Run Code Online (Sandbox Code Playgroud)

  • 使用md5的前8个字符可能有两个URL具有相同散列的合理机会 (30认同)
  • 是的,可能会发生这种冲突,但是随机字符串的几率很小,大约为 1 到 40 亿,但是如果您想拥有 100% 唯一的哈希,您可以将其用作数据库记录使用包含类的参考。 (2认同)
  • 想提一下 `const codeset` 可以是任意顺序,只是为了混淆++ (2认同)

NVR*_*VRM 7

对于简短的哈希url 友好,鉴于不允许可能的重复内容,我们可以使用hash()特别是CRCAdler-32类型,因为它们正是为此而制作的:

循环冗余校验

循环冗余校验 (CRC) 是一种错误检测代码,常用于数字网络和存储设备中,用于检测原始数据的意外更改。根据其内容的多项式除法的余数,进入这些系统的数据块会附加一个简短的检查值。检索时,重复计算,如果检查值不匹配,可以采取纠正措施 https://en.wikipedia.org/wiki/Cyclic_redundancy_check

Adler-32是一种校验和算法(...),与相同长度的循环冗余校验相比,它以可靠性换取速度(更喜欢后者) https://en.wikipedia.org/wiki/Adler-32

echo hash("crc32", "Content of article...");
// Output fd3e7c6e
echo hash("adler32", "Content of article...");
// Output 55df075f
Run Code Online (Sandbox Code Playgroud)

[Youtube] CRC 是如何工作的?