PHP短哈希,例如缩短网址的网站


83

我正在寻找一个PHP函数,该函数可以从字符串或文件中创建短哈希,类似于那些诸如tinyurl.com之类的URL缩短网站。

哈希不得超过8个字符。


2
我知道这是一个老问题,但请查看:hashids.org。适用于大多数编程语言
Greg

查看ShortCode库。它正是您想要的。基于基本转换。
阿尼斯(Anis),2015年

除了使用阿德勒-32或CRC32,你不能缩短现代(防冲突)散列多少(即下降到8个字符)。不支持SHA-2,不支持SHA-1,甚至不支持MD5。使用Alphabet::convert($hash, Alphabet::HEX, Alphabet::ALPHANUMERIC),您可以将MD5减少到22个(从32个)字符。您想要的是改为使用编码文件的整数ID(例如,从数据库中)(new Id())->encode($id)
CAW

Answers:


47

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


1
这是什么意思(每个字符更多的信息),只是很好奇!
ravisoni 2015年

2
@ravisoni如果使用十进制数字0-9来表示一个号码,则有每经编码字符10个可能值(LD(10)≈3.32位/字符)。但是,如果用Base64字符表示相同的数字,则每个编码字符有64个可能的值(ld(64)= 6位/字符)。因此,使用Base64时,每个编码字符中存储的信息更多,即6位信息而不是3.32位。
Gumbo 2015年

3
如果使用base64,则没有什么可以阻止脚本说出($ i = 0; $ i <999999; $ i ++){$ pageContent = fread(fopen(' yoururl.com /'. base64_encode ($i );}现在我可以访问您数据库中的每个URL

161

TinyURL不会散列任何内容,它使用以36为底的整数(甚至以62为底,使用大小写字母)来指示要访问的记录。

以36为底数的整数:

intval($str, 36);

以36为底的整数:

base_convert($val, 10, 36);

那么,而不是重定向到路由喜欢/url/1234它成为/url/ax代替。这将给您带来比哈希更多的使用效率,因为不会发生冲突。使用此方法,您可以轻松地检查URL是否存在,并在用户不知道它已经存在于数据库中的情况下,在base 36中返回正确的现有ID。

不要散列,请使用其他基础进行此类操作。(它速度更快,可以做成防碰撞的。)


嗨@ RobertK,PHP如何转换包含数字和字母的6位数字字符串?
蒂姆·彼得森

@timpeterson,只需调用intval并传递给定的基数(请参阅我的第一个代码块)。
罗伯特K

@RobertK,但intval()将所有内容转换为数字。我想也许我对如何intval()与执行重定向(如数据库角色)所需的其他步骤连接感到困惑。
蒂姆·彼得森

@timpeterson,这是因为该字符串表示数据库记录的ID。因此,您可以根据传递的ID选择记录。
罗伯特K

@RobertK,我遇到的一个问题intval()是,如果其中$str包含斜杠(/)或破折号(-)。我意识到了on/stuffon-stuff然后on所有人都返回了这个号码887。您是否有解决方案来处理带有斜杠和破折号的URL?
蒂姆·彼得森

83

我写了一个小小的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";
}
m8z2p
8hy5e
uqx83
格兹瓦斯
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;
    }

}

12
这是一个非常聪明的设计= D黄金素数= world.rock()
sova

3
我知道我在评论较旧的帖子。我以为我会提到KevBurnsJr代码运行良好。但是,最近我刚刚从Windows 2003 32位服务器切换到Windows 2008 R2 x64服​​务器,发现我正在复制唯一的哈希。我现在必须找到创建确认代码的另一种方法。
DanielJay 2012年

2
在一些评论者的帮助下,该帖子已更新为使用bcmath,因此现在应该牢固。有人还找到了一种使它可逆的方法,这完全是一种尝试。
KevBurnsJr 2013年

2
web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/…似乎网站已关闭,所以这里是该链接的副本;)
Harinder

4
您应该备份您的网站,或者将其php版本发布在github.com/KevBurnsJr/pseudocrypt下-多么伟大的lib!不想使用像YOURLS或PHURL这样的大型“系统”,只是一个不错的lib来创建短链接,就是这样。谢谢
inorganik

21

最短的哈希是32个字符的长度,如何使用md5哈希的前8个字符

echo substr(md5('http://www.google.com'), 0, 8);

更新:这里是另一个类中找到这里书面特拉维尔帕金斯这需要记录号码并为它创建短哈希。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)));
    }
}

这是如何使用它的示例:

BaseIntEncoder::encode('1122344523');//result:3IcjVE
BaseIntEncoder::decode('3IcjVE');//result:1122344523

32
使用第8个charactors MD5的有可能是具有相同散列两个URL的一个合理的机会
汤姆·黑格

2
是的,可能会发生这种冲突,但是随机字符串的机会很小,大约是十亿到四十亿,如果您想拥有100%唯一的哈希(可以用作数据库记录使用的包含类的引用),该机会就很少。
Nazariy

2
想要提及的const codeset可能是任意顺序,只是为了混淆++
Luis Siquot

3

对于不允许访问重复内容的简短哈希值网址友好),我们可以使用CRC哈希类型,hash()尤其是CRC哈希类型,因为它正是为此而创建的:

循环冗余校验

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

https://zh.wikipedia.org/wiki/Cyclic_redundancy_check

echo hash("crc32", "Content of article...");
// Output fd3e7c6e

2

最佳答案:给定唯一的数据库ID,最小的唯一“类似哈希”字符串-PHP解决方案,无需第三方库。

这是代码:

<?php
/*
THE FOLLOWING CODE WILL PRINT:
A database_id value of 200 maps to 5K
A database_id value of 1 maps to 1
A database_id value of 1987645 maps to 16LOD
*/
$database_id = 200;
$base36value = dec2string($database_id, 36);
echo "A database_id value of $database_id maps to $base36value\n";
$database_id = 1;
$base36value = dec2string($database_id, 36);
echo "A database_id value of $database_id maps to $base36value\n";
$database_id = 1987645;
$base36value = dec2string($database_id, 36);
echo "A database_id value of $database_id maps to $base36value\n";

// HERE'S THE FUNCTION THAT DOES THE HEAVY LIFTING...
function dec2string ($decimal, $base)
// convert a decimal number into a string using $base
{
    //DebugBreak();
   global $error;
   $string = null;

   $base = (int)$base;
   if ($base < 2 | $base > 36 | $base == 10) {
      echo 'BASE must be in the range 2-9 or 11-36';
      exit;
   } // if

   // maximum character string is 36 characters
   $charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

   // strip off excess characters (anything beyond $base)
   $charset = substr($charset, 0, $base);

   if (!ereg('(^[0-9]{1,50}$)', trim($decimal))) {
      $error['dec_input'] = 'Value must be a positive integer with < 50 digits';
      return false;
   } // if

   do {
      // get remainder after dividing by BASE
      $remainder = bcmod($decimal, $base);

      $char      = substr($charset, $remainder, 1);   // get CHAR from array
      $string    = "$char$string";                    // prepend to output

      //$decimal   = ($decimal - $remainder) / $base;
      $decimal   = bcdiv(bcsub($decimal, $remainder), $base);

   } while ($decimal > 0);

   return $string;

}

?>

1

实际上,拥有“随机”哈希的最佳解决方案是生成一个随机哈希列表,并使用唯一的INDEX将其放在Mysql上(您可以编写一个简单的UDF在1秒内插入10万行)。

我认为这样的结构ID | HASH | STATUS | URL | VIEWS | ......

状态指示此哈希是否免费。


0

在数据库中进行重复检查的简单方法:

$unique = false;

// While will be repeated until we get unique hash
while($unique == false) {

    // Getting full hash based on random numbers
    $full_hash = base64_encode( rand(9999,999999) ); 

    // Taking only first 8 symbols
    $hash = substr($full_hash, 0, 8); 

    // Checking for duplicate in Database - Laravel SQL syntax
    $duplicate = \App\Item::where('url', $hash)->count(); 

    // If no Duplicate, setting Hash as unique
    if ($duplicate==0) {

        // For stoping while
        $unique=true;

        // New Hash is confirmed as unique
        $input['url']=$hash; 
    }
}

0

我正在缩短网址。就我而言,每次使用唯一的短网址时,我都使用数据库的“ id”来创建。

首先,我所做的是-

在数据库中插入“原始网址”和“创建日期”之类的数据,而在数据库中保留“短网址”为空。然后从那里获取“ id”并传递下面的函数。

<?php
    function genUniqueCode($id){
    $id = $id + 100000000000;
    return base_convert($id, 10, 36);
}

//Get Unique Code using ID
/*
id Below is retrived from Database after Inserting Original URL.
*/



$data['id'] =10;
$uniqueCode = genUniqueCode($data['id']);

   // Generating the URL
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';
echo "<a href='{$protocol}://{$_SERVER['HTTP_HOST']}/{$uniqueCode}'>{$protocol}://{$_SERVER['HTTP_HOST']}/{$uniqueCode}</a>";

?>

然后在数据库中更新短网址代码的值。

在这里,我使用“ id”来创建短代码。由于多个条目的ID不能相同。它是唯一的,因此唯一代码或网址将是唯一的。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.