Short chain generation and chain generation

Source: Internet
Author: User

Short chain generation and chain generation

1 /// <summary> 2 // short chain generation 3 /// </summary> 4 public class ShortUrlBuilder 5 {6 private static readonly string [] Chars = 7 {" ", "B", "c", "d", "e", "f", "g", "h", 8 9 "I", "j ", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t ", 10 11 "u", "v", "w", "x", "y", "z", "0", "1", "2 ", "3", "4", "5", 12 13 "6", "7", "8", "9", "A", "B ", "C", "D", "E", "F", "G", "H", 14 15 "I", "J", "K", "L", "M", "N", "O", "P", "Q ", "R", "S", "T", 16 17 "U", "V", "W", "X", "Y", "Z" 18 }; 19 20 /// <summary> 21 /// generate the summary of the specified URL 22 /// </summary> 23 // <param name = "url"> URL </ param> 24 // <returns> short chain list, just take one storage. </Returns> 25 public static List <string> Build (string url) 26 {27 var shortUrls = new List <string> (); 28 if (url = null) 29 {30 return shortUrls; 31} 32 var hash = GetMd5 (url); 33 // divide the md5 code of 32 characters into four segments for processing, each segment contains 8 characters 34 for (int I = 0; I <4; I ++) 35 {36 37 int offset = I * 8; 38 39 string sub = hash. substring (offset, 8); 40 41 long sub16 = long. parse (sub, NumberStyles. hexNumber); // convert sub into a hexadecimal number to long42 43 // & 0X 3 FFFFFFF: remove the first two digits, leaving only 30 digits 44 sub16 & = 0X3FFFFFFF; 45 46 StringBuilder sb = new StringBuilder (); 47 // divide the remaining 30 digits into 6 segments, each segment with 5 digits 48 for (int j = 0; j <6; j ++) 49 {50 // get a <= 61 digit 51 long t = sub16 & 0x0000003D; 52 sb. append (Chars [(int) t]); 53 54 sub16> = 5; // shifts sub16 right to 5 digits 55} 56 57 shortUrls. add (sb. toString (); 58} 59 return shortUrls; 60} 61 62 // <summary> 63 // MD5 encryption 64 // </summary> 65 // <param name = "myString"> Original string </param> 66 // <param name = "salt"> salt </param> 67 // <returns> </returns> 68 private static string GetMd5 (string myString, string salt = "") 69 {70 if (myString! = Null) 71 {72 var array = myString. toCharArray (); 73 Array. reverse (array); 74 myString = $ "{new string (array)} + {{{ salt }}}"; 75 76 # if DEBUG77 Debugger. log (1, "2", $ "custom message: {myString} \ r \ n"); 78 # endif79 MD5 = MD5.Create (); 80 byte [] bs = Encoding. UTF8.GetBytes (myString); 81 byte [] hs = md5.ComputeHash (bs); 82 StringBuilder sb = new StringBuilder (); 83 foreach (byte B in hs) 84 {85 // format 86 sb in hexadecimal format. append (B. toString ("x2"); 87} 88 return sb. toString (); 89 90} 91 return null; 92} 93}

 

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.