What is short-chain or short-address?
As the name implies, is to turn the long URL into a short URL, now provide this service has a lot of companies, we take the Google home URL shortener service: http://goo.gl/for example. First we go to http://goo.gl/, then we enter the address of this blog Http://blog.csdn.net/beiyeqingteng, and finally it returns a shorter url,http://goo.gl/jfs6q.
Why is Sina Weibo using short chain?
- No matter how long the microblog, can be converted to a fixed length of short chain, to prevent some connections too long affect the user input other content;
- All short chains in the algorithm can not directly solve the chain, must go through Sina's server, the link system to control their own hands. This is very important to the network content censorship, if someone sends the micro-blog contains sensitive content, Sina will not relay;
- Reorganize the content of linked pages to facilitate users to view on the phone;
- Because the long chain may contain the # or the @ These special characters, to the client's string processing brings pressure, the code can eliminate these special symbols;
- Since all links are on Sina's servers, the server maintains links to facilitate data mining and statistical analysis.
Java implementation
1 PackageCom.wanda.restful.framework.test.shorturl;2 3 Importjava.security.MessageDigest;4 5 /**6 * @versionv1.0.07 * @author: Wangyang8 * date:2016-9-8 time:15:329 */Ten Public classEncript { One //hexadecimal number-to-character mapping array A Private Final StaticString[] hexdigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; - - /**encrypt the inputstring .*/ the Public Staticstring MD5 (string inputstr) { - returnencodeByMD5 (INPUTSTR); - } - + /** - * Verify that the password you entered is correct + * @paramPassword real password (encrypted true password) A * @paraminputstring the string entered at * @returnvalidation result, Boolean type - */ - Public Static BooleanAuthenticatepassword (String password,string inputstring) { - if(Password.equals (encodeByMD5 (inputstring))) { - return true; -}Else{ in return false; - } to } + - /**MD5 encoding A string*/ the Private Staticstring encodeByMD5 (String originstring) { * if(originstring!=NULL) { $ Try {Panax Notoginseng //creates a summary of information with the specified algorithm name -MessageDigest MD5 = messagedigest.getinstance ("MD5"); the //The summary is last updated with the specified byte array, and the summary calculation is completed + byte[] results =md5.digest (Originstring.getbytes ()); A //turns the resulting byte array into a string return theString result =bytearraytohexstring (results); + returnresult; -}Catch(Exception e) { $ e.printstacktrace (); $ } - } - return NULL; the } - Wuyi /** the * Rotate byte array to hexadecimal string - * @paramB-byte array Wu * @returnhexadecimal string - * About */ $ Private StaticString bytearraytohexstring (byte[] b) { -StringBuffer RESULTSB =NewStringBuffer (); - for(inti=0;i<b.length;i++){ - resultsb.append (bytetohexstring (b[i)); A } + returnresultsb.tostring (); the } - $ //Converts a byte into a string of 16 binary forms the Private StaticString bytetohexstring (byteb) { the intn =b; the if(n<0) then=256+N; - intD1 = n/16; in intD2 = n%16; the returnHEXDIGITS[D1] +HEXDIGITS[D2]; the } About}
1 PackageCom.wanda.restful.framework.test.shorturl;2 3 /**4 * Generator Shorturl5 * The relationship between the long chain and the short chain needs to be preserved in the database to jump to long links by accessing the short chain6 *7 * @versionv1.0.08 * @author: Wangyang9 * date:2016-9-8 time:15:42Ten */ One Public classShorturlgenerator { A Public Static voidMain (string[] args) { -String Slongurl = "http://www.baidu.com"; - for(String shorturl:shorturl (Slongurl)) { the System.out.println (shorturl); - } - } - + Public Staticstring[] shorturl (String URL) { - //You can customize the hybrid key before generating the MD5 encryption word nginx +String key = "Wangyang"; A //the character to use to generate the URL atString[] chars =NewString[]{"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" in }; - //MD5 encryption of incoming URLs toString Smd5encryptresult = (ENCRIPT.MD5 (key +URL)); +String hex =Smd5encryptresult; -string[] Resurl =NewString[4]; the //get 4 sets of short link strings * for(inti = 0; I < 4; i++) { $ //bitwise AND operation of the cryptographic characters according to the 8-bit set of 16-binary and 0x3fffffffPanax NotoginsengString stempsubstring = hex.substring (i * 8, I * 8 + 8); - //this needs to be converted by a long type, because Inteper. parseint () can handle only 31 bits, the first is the sign bit, and if you don't use a long, it will go out of bounds the LongLhexlong = 0x3fffffff & Long.parselong (stempsubstring, 16); +String Outchars = ""; A //loop to get each set of 6-bit strings the for(intj = 0; J < 6; J + +) { + //The obtained value and 0x0000003d are bitwise AND operation, get the character array chars index (specifically need to see chars array length to prevent subscript overflow, note the starting point is 0) - Longindex = 0x0000003d &Lhexlong; $ //Add the acquired characters $Outchars + = chars[(int) index]; - //Bitwise RIGHT Shift 5 bits per loop -Lhexlong = Lhexlong >> 5; the } - //to put a string into the output array of the corresponding indexWuyiResurl[i] =Outchars; the } - returnResurl; Wu } -}
Resources
- The principle and implementation of short URL
- About short address Shorturl in Weibo content
Java implementation of short chain (Shorturl)