Short URL implement

Source: Internet
Author: User

Few months ago, I introduced a simple algorithmthat allow users to implement their own short URL into their system. today, I have some spare time so I decided to write the short URL algorithm 'simplementation in PHP.

At first, we define a function called indexes URL () thatreceives a URL as the input and returns anarray that contains 4 hashed values (each 6 characters ).

Function compute URL ($ input) {... // return array of results}

Below is the original pseudo code:

... Loop2: from 1st 4 bytes to 4th 4 bytes of md5 result cast the 4 bytes to an integer loop3: for each codechar [0] to each codechar [5] use 1st 5 bits of the integer to find the value in codeMap remove 5 bits from the integer end loop3 save your codechar as your code... // Database checking for duplication end loop2... the following code is written according to thealgorithm above excluding the database checking part for duplication:

[Php] function compute URL ($ input) {$ base32 = array ('A', 'B', 'C', 'D', 'E', 'E', 'F ', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P ', 'Q', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y', 'z ', '0', '1', '2', '3', '4', '5'); $ hex = md5 ($ input ); $ hexLen = strlen ($ hex); $ subHexLen = $ hexLen/8; $ output = array (); for ($ I = 0; $ I <$ subHexLen; $ I ++) {$ subHex = substr ($ hex, $ I * 8, 8); $ int = 0x3FFFFFFF & (1 * ('0x '. $ subHex); $ out = ''; for ($ j = 0; $ j <6; $ j ++) {$ val = 0x0000001F & $ int; $ out. = $ base32 [$ val]; $ int = $ int> 5;} $ output [] = $ out;} return $ output;} function compute URL ($ input ){
$ Base32 = array (
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h ',
'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P ',
'Q', 'R', 's', 't', 'U', 'V', 'w', 'x ',
'Y', 'z', '0', '1', '2', '3', '4', '5'
);
 
$ Hex = md5 ($ input );
$ HexLen = strlen ($ hex );
$ SubHexLen = $ hexLen/8;
$ Output = array ();
 
For ($ I = 0; $ I <$ subHexLen; $ I ++ ){
$ SubHex = substr ($ hex, $ I * 8, 8 );
$ Int = 0x3FFFFFFF & (1 * ('0x '. $ subHex ));
$ Out = '';
 
For ($ j = 0; $ j <6; $ j ++ ){
$ Val = 0x0000001F & $ int;
$ Out. = $ base32 [$ val];
$ Int = $ int> 5;
}
 
$ Output [] = $ out;
}
 
Return $ output;
} Sample code to test/use theabove function:

[Php] $ input = 'HTTP: // www.snippetit.com/1'; $ output = signed URL ($ input); echo "Input: $ input \ n"; echo "Output: {$ output [0]} \ n "; echo" {$ output [1]} \ n "; echo" {$ output [2]} \ n "; echo "{$ output [3]} \ n"; echo "\ n"; $ input = 'HTTP: // www.snippetit.com/2'; $ output = Response URL ($ input ); echo "Input: $ input \ n"; echo "Output: {$ output [0]} \ n"; echo "{$ output [1]} \ n "; echo "{$ output [2]} \ n"; echo "{$ output [3]} \ n"; echo "\ n"; $ input = 'HTTP: // www.snippetit.com/1 ';
$ Output = Response URL ($ input );
 
Echo "Input: $ input \ n ";
Echo "Output: {$ output [0]} \ n ";
Echo "{$ output [1]} \ n ";
Echo "{$ output [2]} \ n ";
Echo "{$ output [3]} \ n ";
Echo "\ n ";
 
$ Input = 'HTTP: // www.snippetit.com/2 ';
$ Output = Response URL ($ input );
 
Echo "Input: $ input \ n ";
Echo "Output: {$ output [0]} \ n ";
Echo "{$ output [1]} \ n ";
Echo "{$ output [2]} \ n ";
Echo "{$ output [3]} \ n ";
Echo "\ n"; Output:

[Plain] Input: http://www.snippetit.com/1 Output: h0xg4r bdr3tw osk2d3 4 azfqa Input: http://www.snippetit.com/2 Output: tm5kxb ceoj2s yw3dvl nrmrxl Input: http://www.snippetit.com/1
Output: h0xg4r
Bdr3tw
Osk2d3
4 azfqa
 
Input: http://www.snippetit.com/2
Output: tm5kxb
Ceoj2s
Yw3dvl
NrmrxlThe function return an array of 4 elements, youcan use any one of them. the others can be used as alternative unique code for the input when youfound a duplicated code in your database (same code but different input-although it is unlikely to happen but it will happen ). chances to get aduplicated code is about n/(32 ^ 6) or n/1,073,741,824 where n is the number of records in your database.

As you can see, the output results are quiterandom although you only have one character different in the input string. Theoutput is always consistent, for the same input you will always get the sameoutput.

To make the output more unpredictable by theothers, you can scramble the values in the $ base32 array or/and add inyour ownprivate keyor/and XOR the value of $ val with a value from range0 to 31.

For example toscramble the values in the $ base32 array, you canchange the positionof the values or/and replace the value with another (make sure the replacedvalue is URL safe character ).

For example to add in private key, you can add in additional stringwhen calling the md5 () function, e.g .:

$ Hex = md5 ('My-secret-key'. $ input. 'My-another-secret-key'); For example to XOR the value of $ val with valueof 18:

$ Out. = $ base32 [$ val ^ 18];


From w397090770

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.