Verification code algorithm optimization method

By optimizing the verification code generation algorithm, using mask and bit operations to reduce the number of calls to random number generation, the problem of inefficiency in the existing verification code generation methods is solved, and more efficient verification code generation and more uniform random number allocation is achieved.

CN119939570APending Publication Date: 2025-05-06BEIJING YUNCHUANG SHILIAN TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510038998.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-10
Publication Date
2025-05-06

AI Technical Summary

Technical Problem

In the existing verification code generation methods, the efficiency of random number generation is low, resulting in slower verification code generation, and excessive digits are wasted in the Go language.

Method used

By initializing the random number generator, calculating the mask and the maximum available digits, generating a verification code loop, checking the remaining available digits, extracting the valid random number part, and updating the status, reducing the number of calls generated by random number, and improving efficiency.

Benefits of technology

The number of calls for random number generation is reduced, the efficiency of verification code generation is improved, and the generated random indexes are more efficient on different character sets, avoiding waste of bits.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119939570A_ABST
    Figure CN119939570A_ABST
Patent Text Reader

Abstract

The invention discloses an optimization method of a verification code algorithm. The optimization method comprises the following steps: S1, initializing a random number generator; s2, calculating a mask and a maximum available bit number; s3, generating a verification code cycle; s4, checking the remaining available digits; s5, extracting an effective random number part; and S6, updating the state. The number of calling times of random number generation is reduced, the overall execution efficiency is improved, and the generated random index is more efficient on different character sets.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of verification codes, and in particular to a verification code algorithm optimization method. Background Art

[0002] With the rapid rise of the Internet, people are using Internet technology more and more frequently, and Internet technology has penetrated into all aspects of life. However, the information input into the Internet is easily exploited by criminals, who use various programs and viruses that can crack the residents' information input into the Internet, thereby obtaining the residents' information.

[0003] In order to curb the above undesirable phenomena, verification code technology came into being. CAPTCHA is a fully automated public Turing test to distinguish between computers and humans. Its purpose is to verify whether the user is a real person when the user performs certain operations. By requiring users to complete a simple task (such as identifying a set of distorted characters or clicking on certain specific areas), the verification code can distinguish between humans and robots, thereby enhancing the security of the website.

[0004] Traditional verification codes usually take the form of graphic verification codes, voice verification codes, etc. However, graphic verification codes may have visual impairment problems, and voice verification codes may have noise impact problems. Therefore, it is necessary to design a verification code generation and verification method based on strings to provide a more convenient, flexible, and verification method.

[0005] Defects and shortcomings of the existing technology:

[0006] 1. The current implementation method is to generate random characters according to the specified character set and random number algorithm. The length of the verification code is the number of times the random algorithm needs to cycle. In this way, the length of the verification code will greatly affect the speed of verification code generation;

[0007] 2. In the Go language, the random number generator returns a 63-bit random result. For most current verification code character sets, only 4-6 bits are needed, which will cause too many bits to be wasted. Summary of the invention

[0008] In view of the above problems, the present invention is proposed to provide an optimization method for a verification code algorithm that overcomes the above problems or at least partially solves the above problems.

[0009] According to one aspect of the present invention, a method for optimizing a verification code algorithm is provided, the optimization method comprising:

[0010] Step S1: Initialize the random number generator;

[0011] Step S2: Calculate the mask and the maximum number of available bits;

[0012] Step S3: Generate verification code loop;

[0013] Step S4: Check the remaining available bits;

[0014] Step S5: extracting the valid random number part;

[0015] Step S6: Update status.

[0016] Optionally, the step S1: initializing the random number generator specifically includes:

[0017] The 63-bit result returned by the random number is reused multiple times according to the length of the character set and the number of bits of the verification code;

[0018] Specify the verification code character set and verification code length.

[0019] Optionally, the step S2: calculating the mask and the maximum number of available bits specifically includes:

[0020] Shift 1 to the left by the verification code length and subtract one to obtain the mask. Divide 63 by the verification code length to obtain the maximum number of available digits, which is used to determine the method for extracting a valid index from a random number.

[0021] Optionally, the step S3: generating a verification code loop specifically includes:

[0022] Continue looping when the number of characters generated is less than the target length;

[0023] Each time a character is generated, the length of the generated random number will increase until it reaches the specified verification code length.

[0024] Optionally, the step S4: checking the remaining available bits specifically includes:

[0025] Before extracting each character index, check the remaining available bits;

[0026] If the number of available bits remaining is 0, it means that the valid bits in the current cache have been used up;

[0027] When the valid bits are exhausted, a new 63-bit random number is regenerated and the remaining available bits are reset, allowing a new character index to be extracted again.

[0028] Optionally, the step S5: extracting the valid random number part specifically includes:

[0029] Use the mask and random number to perform bitwise AND operation to extract the valid random number part;

[0030] The corresponding random number is taken from the character set as the value of the number of verification code cycles.

[0031] According to the optimization method of a verification code algorithm according to claim 1, it is characterized in that the step S6: updating the status specifically includes: subtracting the used mask bits and reducing the number of remaining available character indexes by 1, in preparation for the next extraction.

[0032] The present invention provides an optimization method for a verification code algorithm, the optimization method comprising: step S1: initializing a random number generator; step S2: calculating a mask and a maximum number of available bits; step S3: generating a verification code loop; step S4: checking the remaining number of available bits; step S5: extracting a valid random number part; step S6: updating a state. The number of calls for random number generation is reduced, the overall execution efficiency is improved, and the generated random index is more efficient on different character sets.

[0033] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.

[0035] Figure 1 A flowchart of a verification code algorithm optimization method provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0036] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0037] The terms "comprises" and "having" and any variations thereof in the description embodiments, claims and drawings of the present invention are intended to cover non-exclusive inclusions, for example, including a series of steps or units.

[0038] The technical solution of the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments.

[0039] like Figure 1 As shown, a verification code algorithm optimization method includes:

[0040] Step S1. Initialize the random number generator: multiplex the 63-bit result returned by the random number according to the character set length and the verification code bit number. First, specify the verification code character set and verification code length.

[0041] Step S2. Calculate the mask and the maximum number of available digits: Shift 1 to the left by the verification code length and subtract 1 to obtain the mask, and divide 63 by the verification code length to obtain the maximum number of available digits. These steps are used to determine the method of extracting a valid index from a random number.

[0042] Step S3. Generate verification code loop: When the number of characters generated is less than the target length, continue looping. Each time a character is generated, the length of the generated random number will increase until the specified verification code length is reached.

[0043] Step S4. Check the remaining available bits: Before each character index is extracted, check the remaining available bits. If the remaining available bits are 0, it means that the valid bits in the current cache have been used up (i.e., no more indexes can be extracted from it). When the valid bits are exhausted, a new 63-bit random number is regenerated and the remaining available bits are reset to allow new character indexes to be extracted again.

[0044] Step S5. Extracting the valid random number part: Use the mask and the random number to perform a bitwise AND operation to extract the valid random number part. Then, take the corresponding random number value from the character set as the value of the verification code cycle number.

[0045] Step S6. Update status: Finally, subtract the used mask bits and reduce the number of remaining available character indexes by 1 to prepare for the next extraction.

[0046] Example 2

[0047] A verification code algorithm optimization method, comprising:

[0048] Step S1, specifying the verification code character set and length: defining the character set charset used by the verification code and the length of the verification code.

[0049] Step S2, initializing the mask and the remaining available bits: calculating the mask mask and the remaining available bits remaining_bits.

[0050] Step S3, generate an initial random number: generate a 63-bit initial random number random_value.

[0051] Step S4, looping to generate characters until the target length is reached: using a while loop to continuously generate characters until the number of generated characters reaches the target length.

[0052] Check the remaining available bits: If remaining_bits is 0, it means that the valid bits in the current cache have been used up, and a new 63-bit random number needs to be regenerated and remaining_bits needs to be reset.

[0053] Extract the valid random number part and perform bitwise AND operation with the mask: extract the valid random number part through random_value&mask.

[0054] Extract the value of the corresponding random number from the character set: extract the corresponding character from the character set according to the extracted random number index.

[0055] Update the mask and the number of remaining available bits: subtract the used mask bits and decrement the number of remaining available character indices.

[0056] Step S5, returning the generated verification code: returning the generated verification code.

[0057] Beneficial effects:

[0058] 1. Reduce the number of calls to generate random numbers

[0059] By generating a 63-bit random number at a time, and then using bit operations to extract multiple random indexes. In each loop, the random number generation method is called only when the number of remaining available character indexes is 0, which can avoid frequent calls to the random number generation function, reduce the number of calls, and improve efficiency.

[0060] 2. Improve overall execution efficiency

[0061] Bitwise operations (with masking and right shifting) are cheaper than frequent calls to the random number generation method, so the processing speed is faster. By generating large numbers (63-bit random numbers) once and using them in blocks, it helps to reduce the overall overhead.

[0062] More even distribution of random numbers

[0063] Finer-grained control: The random number is adjusted through the parameter mask to adjust how many bits are extracted from the random number each time to generate the random number of the verification code index. More fine-grained bit control is used to meet the needs of specific character sets, and in actual applications, the number of bits of idxbite is dynamically adjusted to meet different needs

[0064] 4. Better portability. For example, in some systems, the generated random index can be adjusted according to specific usage requirements to make it more efficient on different character sets.

[0065] The above specific implementation methods further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for optimizing a verification code algorithm, characterized in that: The optimization method comprises: Step S1: Initialize the random number generator; Step S2: Calculate the mask and the maximum number of available bits; Step S3: Generate verification code loop; Step S4: Check the remaining available bits; Step S5: extracting the valid random number part; Step S6: Update status.

2. The method for optimizing a verification code algorithm according to claim 1, characterized in that: The step S1: initializing the random number generator specifically includes: The 63-bit result returned by the random number is reused multiple times according to the length of the character set and the number of bits of the verification code; Specify the verification code character set and verification code length.

3. The method for optimizing a verification code algorithm according to claim 1, characterized in that: The step S2: calculating the mask and the maximum number of available bits specifically includes: Shift 1 to the left by the verification code length and subtract one to obtain the mask. Divide 63 by the verification code length to obtain the maximum number of available digits, which is used to determine the method for extracting a valid index from a random number.

4. The method for optimizing a verification code algorithm according to claim 1, characterized in that: The step S3: generating a verification code cycle specifically includes: Continue looping when the number of characters generated is less than the target length; Each time a character is generated, the length of the generated random number will increase until it reaches the specified verification code length.

5. The method for optimizing a verification code algorithm according to claim 1, characterized in that: The step S4: checking the remaining available bits specifically includes: Before extracting each character index, check the remaining available bits; If the number of available bits remaining is 0, it means that the valid bits in the current cache have been used up; When the valid bits are exhausted, a new 63-bit random number is regenerated and the remaining available bits are reset, allowing a new character index to be extracted again.

6. The method for optimizing a verification code algorithm according to claim 1, characterized in that: The step S5: extracting the valid random number part specifically includes: Use the mask and random number to perform bitwise AND operation to extract the valid random number part; The corresponding random number is taken from the character set as the value of the number of verification code cycles.

7. The method for optimizing a verification code algorithm according to claim 1, characterized in that: The step S6: updating the status specifically includes: subtracting the used mask bits and reducing the number of remaining available character indexes by 1, in preparation for the next extraction.