Java generates random and non-repeated Promotion Code invitation Codes

Source: Internet
Author: User

Java generates random and non-repeated Promotion Code invitation Codes

Welcome to my blog: blog.scarlettbai.com to view more articles

We recently received a requirement to generate promotion codes in batches. First, we know that the special effects of promotion codes are as follows:

1: repeatable

2: cannot be inferred

Our ideas for these two points are roughly divided into the following categories:

1: After a random code is generated each timeCheck whether the database is the same(Access to the database every time, resulting in extremely low efficiency, not recommended)

2: BasisThe primary key of the database is used as the unique key.To disrupt or insert data. For example, if the primary key is 8000001, a random number or letter is generated after three digits (increase or decrease as required) are taken out, and a string such as 8000E0V0S1D is inserted into the primary key value, it can ensure that the Promotion Code is not repeated and cannot be deduced (this method also needs to connect to the database to obtain the primary key, of course, you can pre-generate the required number of primary keys at a time, and then generate the corresponding promotion code to update the database, higher efficiency)

3: ExploitationAlgorithmTo ensure that the value is unique, such as UUID. This article focuses on this method (no need to connect to the database, the efficiency is very high when the algorithm is appropriate)

Implementation

As mentioned above, this article focuses on the use of algorithms to achieve non-repeatability. First, we will think of the simplest method:UUID, Let's take a look at the specific effect:

As shown in the figure,Remove the random string generated by UUID-the length is 32 characters, Although it can ensure that there is almost no repetition and multi-thread concurrency is not repeatedToo longIs not practical.

Now that UUID has been eliminated, let's take a look at the most commonSHAAlgorithm:

We can see that the SHA algorithm also generates obvious strings.Too long, obsolete.

Next I thought of the symmetric encryption algorithmRC4He can ensure that the ciphertext length is the same as the original string length. There is not much to describe about the RC4 algorithm in this article. If you are interested, you can go to google Baidu. There are also a lot of algorithms on the Internet. You can just click Next, hereThe ciphertext is an auto-increment number, and the key is written to death.Next, let's take a look at the effect:

The figure shows the generated code.Not in the range of letters and numbers, It is impossible to give these codes to users as promotional codes. The simplest way is to convert the codes to hexadecimal notation. The effect is as follows:

As you can seePassword is a well-known letter and number, but the length is changed to 2 times the length of the original characterBut it is still acceptable, but the encrypted ciphertext is all continuous and cannot be inferred from the second point of the Promotion Code. In addition, because the private keys are all the same, they are prone to cracking, decryption is not required here, so you can directlyReplace the private key with UUIDTo see the effect:

As you can see, the ciphertext generated here can no longer be inferred to meet our needs, so we will test whether it will be repeated.The length of the encrypted string is 7 characters.:

Encryption results are converted in other waysFor the frequently-used letters and numbers, there is no special need for the landlord's place, so we directly use the hexadecimal conversion method.

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.