source

URL 단축 웹사이트와 같은 PHP 단축 해시

factcode 2022. 9. 29. 00:10
반응형

URL 단축 웹사이트와 같은 PHP 단축 해시

tinyurl.com과 같은 URL 단축 웹사이트와 유사한 문자열이나 파일로 짧은 해시를 생성하는 PHP 함수를 찾고 있습니다.

해시는 8자를 초과할 수 없습니다.

TinyURL은 해시 처리를 하지 않고 Base 36 정수(또는 Base 62)를 사용하여 방문할 레코드를 나타냅니다.

베이스 36에서 정수:

intval($str, 36);

베이스 36의 정수:

base_convert($val, 10, 36);

그래서 루트로 리다이렉트 하는 대신/url/1234이 되다/url/ax대신.이렇게 하면 충돌이 발생하지 않으므로 해시보다 훨씬 더 많이 사용할 수 있습니다.이를 통해 URL이 존재하는지 여부를 쉽게 확인하고 사용자가 데이터베이스 내에 이미 존재하는 것을 알지 못하는 사이에 기본 36에 있는 올바른 기존 ID를 반환할 수 있습니다.

허세 부리지 말고 이런 거 다른 거 쓰세요.(더 빠르고 충돌 방지도 가능합니다.)

정수에서 난독화된 해시를 생성하기 위해 작은 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";
}
m8z2p8hy5euqx83gzwas38볼트퍼그6bqtivxzlkk8ro96인치

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;
    }

}

URL 단축 서비스는 오히려 자동 증분 정수 값(보조 데이터베이스 ID 등)을 사용하여 Base64 또는 기타 인코딩으로 인코딩하여 문자당 더 많은 정보를 제공합니다(10자리만 같은 것이 아니라 64자리).

최단 해시는 32자 길이입니다.md5 해시의 처음 8자를 어떻게 사용할 수 있습니까?

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

업데이트: 여기 Travel Perkins가 작성한 다른 클래스가 있습니다.레코드 번호를 취득하여 짧은 해시를 만듭니다.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

url 친화적짧은 해시를 위해 가능한 중복 콘텐츠를 허용하지 않는 관점에서 다음을 사용할 수 있습니다.hash()특히 CRC 또는 Adler-32 타입은 그 목적에 맞게 설계되어 있습니다.

주기적 용장성 검사

CRC(Cyclic Redundancy Check)는 디지털네트워크 및 스토리지 디바이스에서 일반적으로 사용되는 오류 검출 코드입니다.원시 데이터에 대한 우발적인 변경을 검출하기 위해 사용됩니다.이들 시스템에 들어가는 데이터 블록은 그 내용의 다항식 분할의 나머지에 근거해 짧은 체크치를 부가한다.검색 시 계산이 반복되며 체크 값이 일치하지 않을 경우 https://en.wikipedia.org/wiki/Cyclic_redundancy_check에서 수정 조치를 취할 수 있습니다.

Adler-32는 체크섬알고리즘(...)으로 같은 길이의 Cyclic Redundancy Check와 비교하여 신뢰성과 속도(후자)를 교환합니다.https://en.wikipedia.org/wiki/Adler-32

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

[유튜브] CRC의 구조

베스트 답: 고유 데이터베이스 ID가 지정된 최소 고유 "Hash Like" 문자열 - 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;

}

?>

실제로 랜덤 해시를 갖는 가장 좋은 솔루션은 랜덤 해시 목록을 생성하여 고유한 INDEX를 사용하여 Mysql에 올리는 것입니다(1초 만에 100,000 행을 삽입하는 간단한 UDF를 작성할 수 있습니다).

이 ID와 같은 구조라고 생각합니다.해시|상태|URL|VIEWS|......

여기서 status는 이 해시가 빈 상태인지 여부를 나타냅니다.

url shortner를 만들고 있었어요.제 경우 데이터베이스의 "id"를 사용하여 짧은 URL을 만들 때마다 작성했습니다.

내가 한 일은, 우선...

"Original url" 및 "creation date"와 같은 데이터를 db에 삽입하고 "short url"은 db에 비워둡니다.그런 다음 거기서 "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>";

?>

그런 다음 데이터베이스의 Short URL Code 값을 업데이트합니다.

여기서는 "id"를 사용하여 짧은 코드를 만듭니다.여러 항목에 대해 ID가 같을 수 없기 때문입니다.고유하기 때문에 고유 코드 또는 URL이 고유합니다.

알고리즘 같은 걸 썼는데

그건…

  1. 이해하기 쉽고, 조정하고, 변경하기 쉽다
  2. 사용자가 허용하는 기호만 사용합니다(대소문자를 쉽게 구분할 수 있습니다).
  3. 또한 유일무이한
  4. 상자부터 무감각한 케이스
  5. 정의 int(ID)의 경우에만
<?php

class PseudoCrypt1
{
    private static $keychars = 'CZPXD5H2FIWB81KE76JY93V4ORLAMT0QSUNG'; // Dictionary of allowed unique symbols, shuffle it for yourself or remove unwanted chars (don't forget to call testParameters after changing)
    private static $divider = 19; // Tune divider for yourself (don't forget to call testParameters after changing)
    private static $biasDivider = 14; // Tune bias divider for yourself (don't forget to call testParameters after changing)
    private static $noise = 53; // Any positive number

    public static function testParameters()
    {
        if (strlen(static::$keychars) < static::$divider + static::$biasDivider - 1) {
            throw new Exception('Check your divider and biasDivider. It must be less than keychars length');
        }
    }

    public static function encode(int $i): string
    {
        if ($i < 0) {
            throw new Exception('Expected positive integer');
        }

        $keychars = static::$keychars;
        $i = $i + static::$noise; // add noise to a number
        $bias = $i % static::$biasDivider;

        $res = '';

        while ($i > 0) {
            $div = $i % static::$divider;
            $i = intdiv($i, static::$divider);
            $res .= $keychars[$div + $bias];
        }

        // Current version of an algorithm is one of these chars (if in the future you will need to identify a version)
        // Remember this chars on migrating to a new algorithm/parameters
        $res .= str_shuffle('LPTKEZG')[0];
        $res .= $keychars[$bias]; // Encoded bias

        return $res;
    }

    public static function decode($code)
    {
        $keychars = static::$keychars;
        $biasC = substr($code, -1);
        $bias = strpos($keychars, $biasC);
        $code = substr($code, 0, -2);
        $code = str_split(strrev($code));

        $val = 0;

        foreach ($code as $c) {
            $val *= static::$divider;
            $val += strpos($keychars, $c) - $bias;
        }

        return $val - static::$noise;
    }
}

산출량

36926 -> 7IWFZX
927331 -> F4WIKP2
9021324 -> AT66R7P1

다음과 같은 간단한 테스트로 테스트할 수 있습니다(고유성 테스트는 포함되지 않지만 알고리즘은 고유합니다).

PseudoCrypt1::testParameters();

for ($i = 4000000; $i < 9500000; $i++) {
    $hash = PseudoCrypt1::encode($i);
    echo $i.':'.strlen($hash).':'.$hash.PHP_EOL;
    if ($i != PseudoCrypt1::decode($hash)) {
        echo 'FAIL:'.$i.PHP_EOL;
        die();
    }
}

언급URL : https://stackoverflow.com/questions/959957/php-short-hash-like-url-shortening-websites

반응형