This is a created article in which the information may have evolved or changed.
Short Link Service
In some applications, it is often necessary to include a link to open the actual page, which may be very long (because there may be many parameters). This kind of sharing is not only beautiful, but in some platforms will be limited, such as Weibo 140 words. At this point we need to use a short link service.
The short chain service is actually a 1-to-n mapping to long links. When you access a short chain, you can access the actual page by jumping through the app or Web server. We only need to map the long chain, store such correspondence, and realize the short chain generation service.
Algorithm
Maps any long link to a 6-bit character-length string without causing a duplicate of the short link. (not absolute, within a large number of orders of magnitude)
- The original long link is MD5 checksum computed to generate a 32-bit string.
- Divide the 32-bit string by 8 bits to get 4 strings. Converts each string (16 binary form) to an integer value, and the bitwise AND operation of the 0X3FFFFFFF (30 bit 1) generates a 30-bit numeric value.
- The above generated 30-bit values are extracted in 5-bit units, and the resulting values and 0x0000003d are bitwise AND, obtaining an integer value of 0-61, as an index to extract characters from the character array. A short chain of 6 characters is generated.
- A total of 4 short chains can be generated from the 4-segment string.
Realize
Https://github.com/by-zhang/s ... (For star: )