Method and device for constructing verification value of any length and storage medium

By selecting multiple hash algorithms to construct a hash value of any length as a check value, the problem of the hash value being too long is solved, the verification needs of different message format structures are met, and the applicability of hash value is improved.

CN120301653APending Publication Date: 2025-07-11MATRICTIME DIGITAL TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510467301.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-15
Publication Date
2025-07-11

AI Technical Summary

Technical Problem

The hash value calculated by the existing hash algorithm is too long to meet the existing reserved verification message length requirements, resulting in the inability to directly apply to some message format structures.

Method used

By selecting multiple hash algorithms, a hash value of any length is constructed as a check value, and the practice of reserving the check value space for adapting to the algorithm is abandoned, and instead constructing the check value of the corresponding length according to business needs.

Benefits of technology

It realizes the construction of arbitrary length verification values according to business needs, meets the requirements of reserved space, and improves the applicability and flexibility of hash values.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120301653A_ABST
    Figure CN120301653A_ABST
Patent Text Reader

Abstract

The invention discloses a method and device for constructing a verification value of any length and a storage medium. The method comprises the steps that the length L of the verification value in a message structure is acquired; randomly selecting M Hash algorithms from the N Hash algorithms, and respectively inputting the input values into the M Hash algorithms to obtain M Hash values; and obtaining a verification value with the length of L from the M hash values. According to the method, firstly, M Hash algorithms are selected, due to the fact that polling strategies of the algorithms are different (namely, function parameters are different), different Hash value results can be obtained even under the condition of the same input value, and then M Hash values can be obtained; and respectively selecting the Hash values of various lengths from the M Hash values, and combining the Hash values to obtain a check value of any length. According to the method, a method of reserving a verification value space for adapting to an algorithm is abandoned, and is changed into a method of constructing a verification value with a corresponding length according to a business demand, so that the method has higher value in practical application.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technologies, and particularly to a method, device, and storage medium for constructing a check value of any length. Background Art

[0002] When verifying the integrity of transmitted data, a hash value calculated by a hash algorithm is often used as the check value. By comparing whether the original hash value is the same as the hash value calculated by the receiving party, the integrity of the transmitted data is determined.

[0003] A hash algorithm is an algorithm that compresses information of any length into a fixed length. Different hash algorithms will have different output lengths, and the most common ones are 128 bits and 256 bits. The hash algorithm used for verification determines the number of output bits of the hash value. When a hash algorithm is selected, a fixed output length is determined.

[0004] However, existing business scenarios are diverse. In order to allocate more message length to the message content to be sent in a message structure, there may often not be enough message space reserved for the verification message, or sometimes the length reserved for the verification message does not exactly meet the output length of any current hash algorithm, that is, there is a need for a check value of any length.

[0005] For example, the hash values calculated by currently common hash algorithms are 32 bits, 64 bits, etc. However, in some message format structures, only 8 or 16 bits of space are reserved for placing the check value, which makes it impossible to directly apply existing hash algorithms.

[0006] In summary, how to solve the problem that the output length of the hash value, i.e., the check value, calculated by existing hash algorithms is too long to meet the length requirements reserved for the verification message currently is a technical problem that the industry currently focuses on. Summary of the Invention

[0007] Object of the Invention: The object of the present invention is to provide a method, device, and storage medium for constructing a check value of any length, which solves the problem that the output length of the hash value, i.e., the check value, calculated by existing hash algorithms is too long to meet the length requirements reserved for the verification message currently. The present invention first selects different hash algorithms and then constructs a hash value of any length as the check value of the message, abandoning the practice of reserving check value space to adapt to the algorithm and changing it to the practice of constructing a check value of the corresponding length according to business requirements, which is more valuable in practical applications.

[0008] Technical Solution: A method for constructing a check value of any length according to the present invention includes the following steps:

[0009] Obtain the length L of the check value in the message structure;

[0010] Randomly select M hash algorithms from N hash algorithms, and input the input value into the M hash algorithms respectively to obtain M hash values;

[0011] Then obtain a check value with a length of L from the M hash values.

[0012] Furthermore, the N hash algorithms include traditional hash algorithms and quantum hash algorithms; the traditional hash algorithms are one or more of MD4, MD5, SHA2, SHA3, RIPEMD, and MASH-1 algorithms; the quantum hash algorithm is a hash function based on a linear feedback shift register obtained by selecting an irreducible polynomial and an input random number.

[0013] Furthermore, the obtaining a check value with a length of L from the M hash values means:

[0014] Select a hash value with a length of L1 from the first preset position of the first hash value among the M hash values, select a hash value with a length of L2 from the first preset position of the second hash value among the M hash values, and so on, select a hash value with a length of LM from the first preset position of the Mth hash value among the M hash values; wherein, the sum of the lengths of L1 to LM is L;

[0015] Then splice the hash value with a length of L1, the hash value with a length of L2... and the hash value with a length of LM to obtain a hash value with a length of L, and this hash value with a length of L is the check value.

[0016] Furthermore, the first preset position is the initial position of the hash value or the middle position of the hash value; the lengths of L1, L2... and LM are the same or different.

[0017] Furthermore, the obtaining a check value with a length of L from the M hash values means:

[0018] Generate a random number S1;

[0019] Obtain the length P1 of the first hash value among the M hash values, perform a modulo operation on the random number S1 with respect to the length P1 to obtain a remainder m1, then find the m1th bit of the first hash value and select a hash value with a length of L1 from the m1th bit; obtain the length P2 of the second hash value among the M hash values, perform a modulo operation on the random number S1 with respect to the length P2 to obtain a remainder m2, then find the m2th bit of the second hash value and select a hash value with a length of L2 from the m2th bit; and so on, obtain the length PM of the Mth hash value among the M hash values, perform a modulo operation on the random number S1 with respect to the length PM to obtain a remainder mM, then find the mMth bit of the Mth hash value and select a hash value with a length of LM from the mMth bit; wherein, the sum of the lengths of L1 to LM is L;

[0020] Then, the hash values of length L1, the hash values of length L2, …, and the hash values of length LM are concatenated to obtain a hash value of length L, and this hash value of length L is the check value.

[0021] Furthermore, obtaining the check value of length L from the M hash values means:

[0022] Select a hash value of length L from the second preset position of the first hash value among the M hash values, select a hash value of length L from the second preset position of the second hash value among the M hash values, and so on, select a hash value of length L from the second preset position of the Mth hash value among the M hash values;

[0023] XOR the M hash values of length L in sequence in order, and finally the obtained hash value of length L is the check value.

[0024] Furthermore, the second preset position is the initial position of the hash value or the middle position of the hash value.

[0025] Furthermore, obtaining the check value of length L from the M hash values means:

[0026] Generate a random number S2;

[0027] Obtain the length K1 of the first hash value among the M hash values, perform a modulo operation on the length K1 with the random number S2 to obtain a remainder A1, then search for the A1th bit of the first hash value and select a hash value of length L from the A1th bit; obtain the length K2 of the second hash value among the M hash values, perform a modulo operation on the length K2 with the random number S2 to obtain a remainder A2, then search for the A2th bit of the second hash value and select a hash value of length L from the A2th bit; and so on, obtain the length KM of the Mth hash value among the M hash values, perform a modulo operation on the length KM with the random number S2 to obtain a remainder AM, then search for the AMth bit of the Mth hash value and select a hash value of length L from the AMth bit;

[0028] XOR the M hash values of length L in sequence in order, and finally the obtained hash value of length L is the check value.

[0029] The present invention further includes a computer device, which at least includes a processor and a memory. When the processor executes the computer program stored in the memory, it implements the method for constructing a check value of any length as described in any one of the above claims.

[0030] The present invention further includes a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the method for constructing a check value of any length as described in any one of the above claims.

[0031] Advantages of the present invention: In order to meet the reserved space for placing the check value, the present invention first selects M hash algorithms. Since the polling strategies of the algorithms are different (i.e., the function parameters are different), different hash value results will be obtained even under the same input value, so M hash values can be obtained; then, hash values of various lengths are respectively selected from the M hash values for combination to obtain a check value of any length. The present invention abandons the practice of reserving check value space to adapt to the algorithm and instead constructs a check value of the corresponding length according to business requirements, which is more valuable in practical applications. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] Figure 1 It is a schematic flowchart of the method for constructing a check value of any length according to the present invention;

[0033] Figure 2 It is a schematic diagram of splicing to obtain a hash value with length L in Embodiment 1 of the present invention;

[0034] Figure 3 It is a schematic diagram of splicing to obtain a hash value with length L in Embodiment 2 of the present invention;

[0035] Figure 4 It is a schematic diagram of performing exclusive OR on M hash values with length L in Embodiment 3 of the present invention;

[0036] Figure 5 It is a schematic diagram of performing exclusive OR on M hash values with length L in Embodiment 4 of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0037] The present invention will be further described below with reference to the drawings and embodiments:

[0038] Embodiment 1

[0039] As described in the background art, the hash values calculated by existing common hash algorithms are 32 bits, 64 bits, etc. However, in some message format structures, only 8 or 16 bits of space are reserved for placing the check value, which makes it impossible for us to directly apply existing hash algorithms. Therefore, how to solve these problems is an important problem currently faced.

[0040] As Figure 1 shown, in view of this, the present invention proposes a method for constructing a check value of any length, including the following steps:

[0041] Obtain the length L of the check value in the message structure;

[0042] Randomly select M hash algorithms from N hash algorithms, and input the input value into the M hash algorithms respectively to obtain M hash values; the N hash algorithms include traditional hash algorithms and quantum hash algorithms; the traditional hash algorithms are one or more of MD4, MD5, SHA2, SHA3, RIPEMD, and MASH-1 algorithms; the quantum hash algorithm is a hash function based on a linear feedback shift register obtained by selecting an irreducible polynomial and an input random number. For example: M hash algorithms are 2 hash algorithms, both of which are traditional hash algorithms, one is a 32-bit hash algorithm, and the other is a 64-bit hash algorithm. Since their polling strategies are different (i.e., the function parameters are different), even with the same input value, different hash value results will be obtained.

[0043] Then obtain a check value with a length of L from the M hash values, specifically referring to:

[0044] As Figure 2 shown, select a hash value with a length of L1 from the first preset position of the first hash value among the M hash values, select a hash value with a length of L2 from the first preset position of the second hash value among the M hash values, and so on, select a hash value with a length of LM from the first preset position of the Mth hash value among the M hash values; where the sum of the lengths of L1 to LM is L; the first preset position is the initial position of the hash value or the middle position of the hash value; the lengths L1, L2... and LM are the same or different.

[0045] Then concatenate the hash value with a length of L1, the hash value with a length of L2... and the hash value with a length of LM to obtain a hash value with a length of L, and this hash value with a length of L is the check value.

[0046] For example: the length of the check value is 8, the M hash values are 4 hash values, the first preset position is the initial position of the hash value, and the lengths L1, L2... and L4 are the same and are all 2. Then select a hash value with a length of 2 from the initial position of the first hash value, select a hash value with a length of 2 from the initial position of the second hash value, and so on, select a hash value with a length of 2 from the initial position of the fourth hash value;

[0047] Then, concatenate the 4 hash values with a length of 2 in order to obtain a hash value with a length of 8, and this hash value with a length of 8 is the check value.

[0048] In order to meet the reserved space for placing check values, the present invention first selects M hash algorithms. Since the polling strategies of the algorithms are different (i.e., the function parameters are different), different hash value results will be obtained even with the same input value, so M hash values can be obtained. Then, hash values of various lengths are respectively selected from the M hash values for combination to obtain check values of any length. The present invention abandons the practice of reserving check value space to adapt to algorithms and instead constructs check values of corresponding lengths according to business requirements, which is more valuable in practical applications.

[0049] An embodiment of the present invention further provides a computer device, which at least includes a processor and a memory. The memory stores a computer program, and the processor is used to implement the steps in the method for constructing check values of any length in the embodiment of the present invention when executing the computer program stored in the memory.

[0050] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. The computer program is used to implement the steps in the method for constructing check values of any length in the embodiment of the present invention when executed by a processor.

[0051] Embodiment 2

[0052] This embodiment is basically the same as Embodiment 1, except that:

[0053] As Figure 3 shown, in this embodiment, a check value with a length of L is further obtained from the M hash values, specifically referring to:

[0054] Generate a random number S1;

[0055] Obtain the length P1 of the first hash value among the M hash values. Perform a modulo operation on the random number S1 with respect to the length P1 to obtain a remainder m1, and then search for the m1-th bit of the first hash value and select a hash value with a length of L1 from the m1-th bit; obtain the length P2 of the second hash value among the M hash values. Perform a modulo operation on the random number S1 with respect to the length P2 to obtain a remainder m2, and then search for the m2-th bit of the second hash value and select a hash value with a length of L2 from the m2-th bit; and so on. Obtain the length PM of the M-th hash value among the M hash values. Perform a modulo operation on the random number S1 with respect to the length PM to obtain a remainder mM, and then search for the mM-th bit of the M-th hash value and select a hash value with a length of LM from the mM-th bit; where the sum of the lengths of L1 to LM is L;

[0056] Then, splice the hash value with a length of L1, the hash value with a length of L2,... and the hash value with a length of LM to obtain a hash value with a length of L, and this hash value with a length of L is the check value.

[0057] Example: The length of the check value is 8, the number of M hash values is 4 hash values, the lengths L1, L2... and L4 are the same and all 2, the random number S1 is 10, the length P1 of the first hash value is 20. Take the modulus of the random number 10 with respect to the length 20 of the first hash value to obtain the remainder m1 as 10, then look up the 10th position of the first hash value and select a hash value with a length of 2 from the 10th position; obtain the length P2 of the second hash value as 6, take the modulus of the random number 10 with respect to the length 6 of the second hash value to obtain the remainder m2 as 4, then look up the 4th position of the second hash value and select a hash value with a length of 2 from the 4th position; and so on. Obtain the length P4 of the fourth hash value as 8, take the modulus of the random number 10 with respect to the length 8 of the fourth hash value to obtain the remainder m4 as 2, then look up the 2nd position of the fourth hash value and select a hash value with a length of 2 from the 2nd position;

[0058] Then, splice the 4 hash values with a length of 2 in sequence to obtain a hash value with a length of 8, and this hash value with a length of 8 is the check value.

[0059] Example 3

[0060] This example is basically the same as Example 1, the difference is that:

[0061] As Figure 4 shown, in this example, obtain the check value with a length of L from the M hash values, specifically referring to:

[0062] Select a hash value with a length of L from the second preset position of the first hash value among the M hash values, select a hash value with a length of L from the second preset position of the second hash value among the M hash values, and so on, select a hash value with a length of L from the second preset position of the Mth hash value among the M hash values; the second preset position is the initial position of the hash value or the middle position of the hash value.

[0063] XOR the M hash values with a length of L in sequence in order, and finally the hash value with a length of L obtained is the check value.

[0064] Example: The length of the check value is 8, the number of M hash values is 4 hash values, and the second preset position is the middle position of the hash value. Then select a hash value with a length of 8 from the middle position of the first hash value, select a hash value with a length of 8 from the middle position of the second hash value, and so on, select a hash value with a length of 8 from the middle position of the fourth hash value;

[0065] Then, XOR the 4 hash values with a length of 8 in sequence in order, and finally the hash value with a length of 8 obtained is the check value.

[0066] Example 4

[0067] This embodiment is basically the same as Embodiment 1, except that:

[0068] As Figure 5 shown, in this embodiment, a check value with a length of L is obtained from the M hash values, specifically:

[0069] Generate a random number S2;

[0070] Obtain the length K1 of the first hash value among the M hash values, perform a modulo operation on the length K1 with the random number S2 to obtain a remainder A1, then search for the A1-th bit of the first hash value and select a hash value with a length of L from the A1-th bit; obtain the length K2 of the second hash value among the M hash values, perform a modulo operation on the length K2 with the random number S2 to obtain a remainder A2, then search for the A2-th bit of the second hash value and select a hash value with a length of L from the A2-th bit; and so on, obtain the length KM of the M-th hash value among the M hash values, perform a modulo operation on the length KM with the random number S2 to obtain a remainder AM, then search for the AM-th bit of the M-th hash value and select a hash value with a length of L from the AM-th bit;

[0071] XOR the M hash values with a length of L in sequence in order, and the finally obtained hash value with a length of L is the check value.

[0072] Illustrate with an example: The length of the check value is 8, there are 4 hash values among the M hash values, the random number S2 is 20, the length K1 of the first hash value is 30, perform a modulo operation on the length 30 of the first hash value with the random number 20 to obtain a remainder A1 of 20, then search for the 20th bit of the first hash value and select a hash value with a length of 8 from the 20th bit; obtain the length K2 of the second hash value is 20, perform a modulo operation on the length 20 of the second hash value with the random number 20 to obtain a remainder A2 of 0, then search for the 0th bit of the second hash value and select a hash value with a length of 8 from the 0th bit (the number of bits of the hash value starts from 0, for example, if the length of the hash value is 20, then it is from the 0th bit to the 19th bit); and so on, obtain the length K4 of the fourth hash value is 15, perform a modulo operation on the length 15 of the fourth hash value with the random number 20 to obtain a remainder A4 of 5, then search for the 5th bit of the second hash value and select a hash value with a length of 8 from the 5th bit (if the obtained selected hash value with a length is not enough, return to the initial position to loop and obtain);

[0073] Then, XOR the 4 hash values with a length of 8 in sequence in order, and the finally obtained hash value with a length of 8 is the check value.

[0074] In summary, the present invention can construct hash values of any length as the check value of the message by selecting different hash algorithms. The method of constructing the check value with the corresponding length according to the service requirements is more valuable in practical applications and can also be applied to more scenarios.

Claims

1. A method for constructing a check value of any length, characterized in that, Including the following steps: Obtain the length L of the check value in the message structure; Randomly select M hash algorithms from N hash algorithms, and input the input value into the M hash algorithms respectively to obtain M hash values; Then obtain the check value with length L from the M hash values.

2. The method for constructing a check value of any length according to claim 1, wherein: Among the N hash algorithms, there are traditional hash algorithms and quantum hash algorithms; the traditional hash algorithms are one or more of MD4, MD5, SHA2, SHA3, RIPEMD, and MASH-1 algorithms; the quantum hash algorithm is a hash function based on a linear feedback shift register obtained by selecting an irreducible polynomial and inputting a random number.

3. A method for constructing a check value of any length according to claim 1, characterized in that, The obtaining the check value with length L from the M hash values means: Select the hash value with length L1 from the first preset position of the first hash value among the M hash values, select the hash value with length L2 from the first preset position of the second hash value among the M hash values, and so on, select the hash value with length LM from the first preset position of the Mth hash value among the M hash values; where the sum of the lengths of L1 to LM is L; Then splice the hash value with length L1, the hash value with length L2... and the hash value with length LM to obtain a hash value with length L, and this hash value with length L is the check value.

4. A method for constructing a check value of any length according to claim 3, characterized in that: The first preset position is the initial position or the middle position of the hash value; the lengths of L1, L2... and LM are the same or different.

5. A method for constructing a check value of any length according to claim 1, characterized in that The obtaining the check value with length L from the M hash values means: Generate a random number S1; Obtain the length P1 of the first hash value among the M hash values, perform a modulo operation on the random number S1 with respect to the length P1 to obtain a remainder m1, then find the m1th bit of the first hash value and select the hash value with length L1 from the m1th bit; obtain the length P2 of the second hash value among the M hash values, perform a modulo operation on the random number S1 with respect to the length P2 to obtain a remainder m2, then find the m2th bit of the second hash value and select the hash value with length L2 from the m2th bit; and so on, obtain the length PM of the Mth hash value among the M hash values, perform a modulo operation on the random number S1 with respect to the length PM to obtain a remainder mM, then find the mMth bit of the Mth hash value and select the hash value with length LM from the mMth bit; where the sum of the lengths of L1 to LM is L; Then splice the hash value with length L1, the hash value with length L2... and the hash value with length LM to obtain a hash value with length L, and this hash value with length L is the check value.

6. A method for constructing a check value of any length according to claim 1, characterized in that The obtaining the check value with length L from the M hash values means: Select the hash value with length L from the second preset position of the first hash value among the M hash values, select the hash value with length L from the second preset position of the second hash value among the M hash values, and so on, select the hash value with length L from the second preset position of the Mth hash value among the M hash values; XOR the M hash values with length L in sequence, and finally the hash value with length L obtained is the check value.

7. A method for constructing a check value of any length according to claim 6, characterized in that: The second preset position is the initial position or the middle position of the hash value.

8. A method for constructing a check value of any length according to claim 1, characterized in that, Obtaining a check value of length L from the M hash values means: Generating a random number S2; Obtaining the length K1 of the first hash value among the M hash values, taking the modulo operation of the random number S2 with respect to the length K1 to obtain a remainder A1, then looking up the A1-th bit of the first hash value and selecting a hash value of length L from the A1-th bit; obtaining the length K2 of the second hash value among the M hash values, taking the modulo operation of the random number S2 with respect to the length K2 to obtain a remainder A2, then looking up the A2-th bit of the second hash value and selecting a hash value of length L from the A2-th bit; and so on, obtaining the length KM of the M-th hash value among the M hash values, taking the modulo operation of the random number S2 with respect to the length KM to obtain a remainder AM, then looking up the AM-th bit of the M-th hash value and selecting a hash value of length L from the AM-th bit; XORing the M hash values of length L in sequence, and finally the obtained hash value of length L is the check value.

9. A computer device, characterized in that: The computer device at least includes a processor and a memory, and the processor is used to implement the method for constructing a check value of any length as described in any one of claims 1-8 when executing the computer program stored in the memory.

10. A computer-readable storage medium, characterized in that: It stores a computer program, and when the computer program is executed by the processor, it implements the method for constructing a check value of any length as described in any one of claims 1-8.