Because of the company's promotion needs, a short link generation scheme is required, just a few days ago, someone posted a message on MSDN. net version of the short link algorithm, I will convert it to java version, share it out
Generation idea:
1. encrypt "original link (persistent link) + key (custom string to prevent algorithm leakage)" MD5
2. perform bitwise AND operations on the encrypted characters in hexadecimal notation 0x3 fffffff and 0x0000003D, and obtain the chars index of the character array, add the obtained characters, move the values to the right by five places in each loop, and store the strings in the output array of the corresponding index (4 groups of 6-bit strings)
3. Generate a random number less than 4 and extract the string at the corresponding position of the random number from the input array as a short chain, which is stored in the database or NoSql
Resolution Method
Write a web processing program that parses short links from ur (such as: http://url.51bi.com/zAnuAn), matches the resolved short link (zAnuAn) with the original link stored in the database, jump to the matched original link
Package com. bjdata. test; import java. security. messageDigest; import java. util. random; public class ShortUrlTest {public static void main (String [] args) {String sLongUrl = "http://www.51bi.com/bbs/_t_278433840/"; // original link System. out. println ("long link:" + sLongUrl); String [] aResult = slow URL (sLongUrl ); // four 6-bit strings are generated. // print the result for (int I = 0; I <aResult. length; I ++) {System. out. println ("[" + I + "]:" + aResult [I]);} Random random = new Random (); int j = random. nextInt (4); // a random number of less than 4. out. println ("short link:" + aResult [j]); // randomly fetch a short link} public static String [] url (String url) {// you can customize the hybrid KEY String key = "test" before MD5 encrypted characters are passed in "; // use the String [] chars = new String [] {"a", "B", "c", "d", "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", "6", "7", "8 ", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I ", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S ", "T", "U", "V", "W", "X", "Y", "Z "}; // MD5 encryption String hex = md5ByHex (key + url); String [] resUrl = new String [4]; for (int I = 0; I <4; I ++) {// bitwise AND OPERATION String sTempSubString = hex according to a group of octal characters and 0x3FFFFFFF. substring (I * 8, I * 8 + 8); // you need to use the long type for conversion, because Inteper. parseInt () can only process 31 bits. The first part is the symbol bit. If long is not used, long lHexLong = 0x3FFFFFFF & Long. parseLong (sTempSubString, 16); String outChars = ""; for (int j = 0; j <6; j ++) {// bitwise AND operation are performed on the obtained value and 0x0000003D to obtain the character array chars index long index = 0x0000003D & lHexLong; // Add the obtained characters outChars + = chars [(int) index]; // shift the right of each loop by bit by 5 lHexLong = lHexLong> 5 ;} // store the string to the output array resUrl of the corresponding index [I] = outChars;} return resUrl;}/*** MD5 encryption (32-bit capital) * @ param src * @ return */public static String md5ByHex (String src) {try {MessageDigest md = MessageDigest. getInstance ("MD5"); byte [] B = src. getBytes (); md. reset (); md. update (B); byte [] hash = md. digest (); String hs = ""; String stmp = ""; for (int I = 0; I
Running result
Persistent link: bytes