The go language realizes short chain birth algorithm

Source: Internet
Author: User
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)

  1. The original long link is MD5 checksum computed to generate a 32-bit string.
  2. 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.
  3. 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.
  4. A total of 4 short chains can be generated from the 4-segment string.

Realize

Https://github.com/by-zhang/s ... (For star: )

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.