Character string Hash processing method and processing device

文档序号:1744525 发布日期:2019-11-26 浏览:12次 中文

阅读说明:本技术 字符串哈希处理方法及装置 (Character string Hash processing method and processing device ) 是由 李立亚 吴丽 张春燕 于 2019-08-28 设计创作,主要内容包括:本发明提供了一种字符串哈希处理方法及装置,该字符串哈希处理方法包括:获取字符串数据并设置所述字符串数据的哈希码初始值,所述哈希码初始值的末位为奇数;基于所述哈希码初始值对所述字符串数据中的目标字符进行哈希处理,以得到所述目标字符的哈希码值;基于所述目标字符的哈希码值得到所述字符串数据的哈希码值。本发明用以降低哈希码值的重复率,具有良好的哈希效果。(The present invention provides a kind of character string Hash processing method and processing device, which includes: the Hash codes initial value for obtaining string data and the string data being arranged, and the last bit of the Hash codes initial value is odd number;Hash processing is carried out to the target character in the string data based on the Hash codes initial value, to obtain the Hash code value of the target character;Hash codes value based on the target character obtains the Hash code value of the string data.Repetitive rate of the present invention to reduce Hash code value has good Hash effect.)

1. a kind of character string Hash processing method, which is characterized in that the described method includes:

It obtains string data and the Hash codes initial value of the string data is set, the last bit of the Hash codes initial value is Odd number;

Hash processing is carried out to the target character in the string data based on the Hash codes initial value, to obtain the mesh The Hash code value of marking-up symbol;

Hash codes value based on the target character obtains the Hash code value of the string data.

2. the method according to claim 1, wherein based on the Hash codes initial value to the string data In target character carry out Hash processing, to obtain the Hash code value of the target character, comprising:

By the Hash codes initial value to target direction displacement target digit, with the Hash code value after being shifted;

Hash code value after displacement is negated, to obtain negated Hash code value;

Negated Hash code value is carried out being added processing with the target character in the string data, to obtain addition processing Hash code value afterwards;

Based on being added, treated that Hash codes value obtains the target cryptographic Hash of the target character.

3. according to the method described in claim 2, it is characterized in that, based on being added, treated that Hash codes value obtains the word It accords in string data before the target cryptographic Hash of target character, and will be in negated Hash code value and the string data Target character carries out addition processing, to obtain being added after treated Hash code value, further includes:

It determines in the string data with the presence or absence of untreated character;

The untreated character is then carried out Hash processing, to obtain the untreated character by the untreated character if it exists Hash code value;

The Hash code value of the target character, then is determined as the Kazakhstan of the string data by the untreated character if it does not exist Uncommon code value.

4. according to the method described in claim 2, it is characterized in that, by the Hash codes initial value to target direction displacement target Digit, with the Hash code value after being shifted, comprising:

By the Hash codes initial value to left dislocation seven, with the Hash code value after being shifted.

5. the method according to claim 1, wherein based on being added, treated that Hash codes value obtains the character The target cryptographic Hash of target character in string data, comprising:

A high position for the Hash code value that will add up that treated is set as zero, to obtain the target cryptographic Hash of the target character.

6. according to the method described in claim 5, it is characterized in that, a high position for the Hash code value that will add up that treated is set as Zero, to obtain the operation of the target cryptographic Hash of the target character are as follows:

The Hash code value that will add up that treated and preset value carry out position and operation, to obtain the target Hash of the target character Value;

Wherein, the preset value is 0x7fffffff.

7. according to the method described in claim 1, it is characterized by:

The Hash codes initial value is 0x7fa1b3c5.

8. a kind of character string Hash processing unit characterized by comprising

Acquiring unit, for obtaining string data;

Setting unit, for the Hash codes initial value of the string data to be arranged, the last bit of the Hash codes initial value is surprise Number;

Processing unit, for being carried out at Hash based on the Hash codes initial value to the target character in the string data Reason, to obtain the Hash code value of the target character, and the Hash codes value based on the target character obtains the character string number According to Hash code value.

9. a kind of terminal device, comprising: memory, processor and be stored on the memory and can transport on the processor Capable computer program is realized as described in any one of claims 1 to 7 when the computer program is executed by the processor Method the step of.

10. a kind of computer readable storage medium, computer program, the calculating are stored on the computer readable storage medium The step of method as described in any one of claims 1 to 7 is realized when machine program is executed by processor.

Technical field

The present invention relates to Hash processing method field more particularly to a kind of character string Hash processing method and processing devices.

Background technique

Data with arbitrary length block is mapped as the binary value of shorter regular length by hash algorithm, this small binary system Value is known as cryptographic Hash, and cryptographic Hash is the compact numerical value representation of one kind of the data block.As long as changing any word of data block Section, can all generate different cryptographic Hash, find the different inputs of same hashed value, be computationally nearly impossible.Cause This, hash algorithm is widely used on computer digital animation, using data cryptographic Hash can with the integrality of inspection data, Logarithm factually existing quickly lookup and data encryption algorithm combined structure secured data structure etc..

Hash (Hash) algorithm, i.e. hash function are a kind of one-way cipher systems, be one from plaintext to ciphertext can not Inverse mapping, only ciphering process, without decrypting process.Simultaneously hash function can by the input of random length by variation with The output of regular length is obtained afterwards.The fixed feature of this characteristic of unidirectional and output data length of hash function makes it can be with Generate message or data.

The implementation of hash algorithm have addition and subtraction, bit arithmetic, multiplication division, table look-up, mix realization etc., different realizations Mode difference in the speed of service and Hash effect.Relatively common algorithm such as MD5, SHA-1, BKDRHash, APHash Deng.Different hash algorithms has different characteristics, is useful in different occasions, than if any hash algorithm it is highly-safe, have Hash algorithm speed is fast.In many applications, needs to carry out Hash calculation to the data of character string type, be set for character string The hash algorithm of meter to character string carry out Hash calculation effect is more preferable, speed faster.

In view of this, it is necessary to the method for character string Hash in the prior art processing is improved, it is above-mentioned to solve Problem.

Summary of the invention

The purpose of the present invention is to provide a kind of character string Hash processing method and processing devices, to reduce the weight of Hash code value Multiple rate, to improve the safety of Hash codes.

To achieve the above object, the present invention is implemented as follows:

In a first aspect, providing a kind of character string Hash processing method comprising:

It obtains string data and the Hash codes initial value of the string data, the end of the Hash codes initial value is set Position is odd number;

Hash processing is carried out to the target character in the string data based on the Hash codes initial value, to obtain State the Hash code value of target character;

Hash codes value based on the target character obtains the Hash code value of the string data.

Second aspect, the present invention provide a kind of character string Hash processing unit, comprising:

Acquiring unit, for obtaining string data;

Setting unit, for the Hash codes initial value of the string data, the last bit of the Hash codes initial value to be arranged For odd number;

Processing unit, for carrying out Hash to the target character in the string data based on the Hash codes initial value Processing, to obtain the Hash code value of the target character, and the Hash codes value based on the target character obtains the character string The Hash code value of data.

The third aspect, the present invention also provides a kind of terminal devices, comprising: memory, processor and is stored in the storage On device and the computer program that can run on the processor, realized such as when the computer program is executed by the processor The step of method described in first aspect.

Fourth aspect, also a kind of computer readable storage medium of the present invention store on the computer readable storage medium Computer program, the step of method as described in relation to the first aspect is realized when the computer program is executed by processor.

The last bit that character string Hash processing method of the invention passes through setting is the Hash codes initial value of odd number to acquisition Target character in string data carries out Hash processing, the Hash code value of target character is obtained, according to the Kazakhstan of target character Uncommon code value obtains the Hash code value of string data.In this way, not only making to carry out string data by means of the present invention The speed of Hash processing is fast, and enables to the distributivity of obtained Hash code value relatively good, to reduce generated breathe out The repetitive rate of uncommon code value, to improve the safety of Hash code value.

Detailed description of the invention

Fig. 1 is the schematic flow chart according to the character string Hash processing method of one embodiment of the invention;

Fig. 2 is the schematic flow chart according to the character string Hash processing method of another embodiment of the present invention;

Fig. 3 is the schematic flow chart according to the character string Hash processing method of further embodiment of the present invention;

Fig. 4 is the schematic flow chart according to the character string Hash processing method of further embodiment of the present invention;

Fig. 5 is the schematic block diagram according to the character string Hash processing unit of one embodiment of the invention.

Specific embodiment

The present invention is described in detail for each embodiment shown in reference to the accompanying drawing, but it should be stated that, these Embodiment is not limitation of the present invention, those of ordinary skill in the art according to these embodiments made by function, method, Or equivalent transformation or substitution in structure, all belong to the scope of protection of the present invention within.

The technical solution provided below in conjunction with attached drawing, each embodiment that the present invention will be described in detail.

Fig. 1 is according to the schematic flow chart of the character string Hash processing method of one embodiment of the invention, and character string is breathed out Uncommon processing method, method include:

Step 102. obtains the Hash codes initial value of string data and the data that setup string, the end of Hash codes initial value Position is odd number.

Step 104. carries out Hash processing to the target character in string data based on Hash codes initial value, to obtain mesh The Hash code value of marking-up symbol.

Step 106. obtains the Hash code value of string data based on the Hash codes value of target character.

Since the Hash codes that the last bit that the character string Hash processing method of the embodiment of the present invention passes through setting is odd number are initial It is worth and Hash processing is carried out to the target character in the string data of acquisition, the Hash code value of target character is obtained, according to mesh The Hash codes value of marking-up symbol obtains the Hash code value of string data.In this way, method through the embodiment of the present invention not only makes The speed for carrying out Hash processing to string data is fast, and enables to the distributivity of obtained Hash code value relatively good, To reduce the repetitive rate of generated Hash code value, to improve the safety of Hash code value.

Applicants have unexpectedly found that the last bit of Hash codes initial value is set as odd number, finally obtained character can be made The distributivity of the Hash code value of string data is relatively good, i.e. the repetitive rate of the Hash code value of string data is relatively low.In particular, will Hash codes initial value can be set to 0x7fa1b3c5, can further decrease the repetitive rate of the Hash code value of string data, To further increase the safety of Hash code value.

In the above-described embodiments, as shown in Fig. 2, being carried out based on Hash codes initial value to the target character in string data Hash processing, to obtain the Hash code value of target character, comprising:

Step 202. by Hash codes initial value to target direction displacement target digit, with the Hash code value after being shifted. For the Hash code value (such as 0x7fa1b3c5) of 32 word lengths, it is preferable that can be by Hash codes initial value to left dislocation seven Position, with the Hash code value after being shifted, it is of course also possible to by Hash codes initial value to left dislocation 1-6.It needs to illustrate It is that the maximum value for the target digit for being displaced Hash codes initial value to target direction depends on the digit of Hash codes initial value, i.e., When Hash codes initial value is 32 word lengths, optimal by target digit that Hash codes initial value is displaced to target direction is 7 (i.e. To left dislocation 7);When Hash codes initial value is 64 word lengths, target position that Hash codes initial value is displaced to target direction It is several it is optimal be 7 (i.e. to left dislocation 7), and the Hash codes initial value of 64 word lengths is set still according to 0x7fa1b3c5 rule It sets, i.e., the Hash codes initial value of 64 word lengths can be set to 0x7fa1b3c57fa1b3c5;Etc., it different one illustrates herein It is bright, it can be seen that, the maximum value for the target digit that Hash codes initial value is displaced to target direction depends on Hash codes initial value Digit is not limited to protection scope defined by the embodiment of the present invention.

Step 204. negates the Hash code value after displacement, to obtain negated Hash code value.

Step 206. carries out negated Hash code value to be added processing with the target character in string data, to obtain It is added treated Hash code value.

Treated that Hash codes value obtains the target cryptographic Hash of target character based on being added for step 208..Common program design Character types length in language is 1 byte or 2 bytes, can directly carry out adding operation using character.Consider different journeys The difference of sequence design language integer data type, such as java applet design language do not have signless integer, to guarantee compatibility, Make full use of programming language automatic type to convert, save expense, can in application Uniform provisions by byte automatic conversion For signed integer.

It should be understood that then carrying out phase with the target character in string data by being shifted, being negated to Hash code value Add processing operation, can mix and upset string data, so, it is possible the repetitive rate for reducing the Hash code value generated, improve The safety of the Hash code value of acquired string data.Applicants have unexpectedly found that by Hash codes initial value to left dislocation seven It is negated behind position and is added processing, the distributed effect of the Hash code value of finally obtained string data can be made more preferable, So as to further increase the safety of Hash code value.

In above-mentioned further embodiment, as shown in figure 3, based on being added, treated that Hash codes value obtains character string In data before the target cryptographic Hash of target character, and by the target character in negated Hash code value and string data Addition processing is carried out, to obtain being added the operation after treated Hash code value further include:

Step 302., which determines, whether there is untreated character in string data.

Untreated character is then carried out Hash processing, to obtain untreated character by step 304. untreated character if it exists Hash code value;The Hash code value of target character is then determined as the Hash codes of string data by untreated character if it does not exist Value.

It should be understood that in finding out string data after the Hash code value of target character, for still being deposited in string data Untreated character, then need to continue through Hash processing (concrete processing procedure can be found in the handling principle in Fig. 2), wait for a right man to marry It accords with when untreated character being not present in string data, it at this time can will treated that the corresponding Hash code value of target character is true by Hash It is set to the Hash code value of string data, to complete the Hash evaluation to string data.Since by Hash, treated Hash code value is acquired after mixing and upsetting string data, therefore can reduce the repetition of the Hash code value of generation Rate, the safety of the Hash code value of string data obtained by improving.

Wherein, as shown in figure 4, treated that Hash codes value obtains the target of target character in string data based on being added Cryptographic Hash, comprising:

A high position for step 2081. will add up that treated Hash code value is set as zero, is breathed out with obtaining the target of target character Uncommon value.

Specifically, a high position for the Hash code value that will add up that treated is set as zero, breathed out with obtaining the target of target character The operation of uncommon value can be with are as follows:

The Hash code value that will add up that treated and preset value carry out position and operation, to obtain the target Hash of target character Value.Wherein, preset value 0x7fffffff.

I.e. by the high position zero for the Hash code value that will add up that treated, being equivalent to will add up that treated Hash code value turns It is changed to positive value, the target cryptographic Hash positive number of the target character made, to determine word according to the target cryptographic Hash of target character Accord with the cryptographic Hash of string data.

The character string Hash processing method of the embodiment of the present invention is directed to the Hash calculation demand of string data, uses shifting Position, negate with three kinds of basic operations of addition, devise the hash method of processing character string type data, this method not only handles speed Degree is than very fast, and obtained Hash result distributivity is very good.Applicant have surprisingly discovered that with BKDRHash algorithm is only passed through Scheme compare, the scheme fast 8% or so of the processing speed ratio BKDRHash algorithm of the method for the embodiment of the present invention, wherein right Than testing 26,000,000 character strings, the Hash codes that two hash algorithms calculate do not have repetition.As it can be seen that the word of the embodiment of the present invention Symbol string Hash processing method can not only improve the speed that Hash processing is carried out to string data, additionally it is possible to reduce the Kazakhstan of generation The repetitive rate of uncommon code value, the safety of the Hash code value of string data obtained by improving.

In one particular embodiment of the present invention, the workflow of character string Hash processing method can be with are as follows:

Step 1 gets out string data, and setting Hash codes initial value is 0x7fa1b3c5.

Step 2: moving to left 7 for current Hash code value, and high 7 are given up, and low 7 are filled out 0, the Hash codes after obtaining displacement Value.

Step 3: the Hash code value after shifting in step 2 is negated, obtains negated Hash code value.

Step 4: by the target character (character to be calculated) in Hash code value negated in step 3 and string data It is added, obtains Hash code value after being added.Character types length in common program design language is 1 byte or 2 bytes, It can directly carry out adding operation using character.Consider the difference of distinct program design language integer data type, such as Java journey Sequence design language does not have signless integer, to guarantee compatibility, programming language automatic type is made full use of to convert, saving is opened Byte can be automatically converted to signed integer by Uniform provisions in application by pin.

Step 5: it determines in string data with the presence or absence of untreated character, and if it exists, then return step two continues to execute Processing, if it is not, then entering step six.

Step 6: the Hash code value high position obtained after will add up is set as 0, that is, make after being added obtained Hash code value with 0x7fffffff carries out position and calculates, and the Hash code value obtained after calculating is then the Hash code value of final string data.

Since the character string Hash processing method of the embodiment of the present invention is by setting Hash codes initial value, calculated in Hash codes In the process, shifted left repeatedly to Hash codes, negate and be added calculating with character, until calculating the Hash codes of string data Value, it is seen then that the process flow of character string Hash processing method is simple, and remembers, programs and realize all very simply.In this way, passing through The method of the embodiment of the present invention not only makes the speed for carrying out Hash processing to string data fast, but also enables to acquired Hash code value distributivity it is relatively good, with the repetitive rate of Hash code value caused by reducing, to improve the peace of Hash code value Quan Xing.

As shown in figure 5, the embodiment of the present invention also provides a kind of character string Hash processing unit, comprising: acquiring unit 502, For obtaining string data;Setting unit 504, for the Hash codes initial value for the data that setup string, Hash codes initial value Last bit be odd number;Processing unit 506, for carrying out Hash to the target character in string data based on Hash codes initial value Processing, the Hash codes value to obtain the Hash code value of target character, and based on target character obtain the Hash codes of string data Value.

Since the character string Hash processing unit of the embodiment of the present invention is odd number by the last bit that setting unit 504 is arranged Hash codes initial value carries out Hash processing to the target character in the string data obtained by acquiring unit 502, to pass through Processing unit 506 obtains the Hash code value of target character, and obtains the Kazakhstan of string data according to the Hash codes value of target character Uncommon code value.In this way, device through the embodiment of the present invention not only makes the speed for carrying out Hash processing to string data fast, and And enable to the distributivity of obtained Hash code value relatively good, to reduce the repetitive rate of generated Hash code value, thus Improve the safety of Hash code value.

In the above-described embodiments, processing unit 506 is also used to Hash codes initial value to target direction displacement target digit, With the Hash code value after being shifted, the Hash code value after displacement is negated, to obtain negated Hash code value, and will Negated Hash code value carries out being added processing with the target character in string data, obtains being added treated Hash codes Value, with treated that Hash codes value obtains the target cryptographic Hash of target character based on being added.Wherein, processing unit 506 can be with For by Hash codes initial value to left dislocation seven, with the Hash code value after being shifted.

It should be understood that then carrying out phase with the target character in string data by being shifted, being negated to Hash code value Add processing operation, can mix and upset string data, so, it is possible the repetitive rate for reducing the Hash code value generated, improve The safety of the Hash code value of acquired string data.Applicants have unexpectedly found that by Hash codes initial value to left dislocation seven It is negated behind position and is added processing, the distributed effect of the Hash code value of finally obtained string data can be made more preferable, So as to further increase the safety of Hash code value.

In above-mentioned further embodiment, character string Hash processing unit further includes determination unit 508, for determining word It accords with and whether there is untreated character in string data.Processing unit 506 for untreated character if it exists, then by untreated character into Row Hash processing, to obtain the Hash code value of untreated character;Untreated character if it does not exist, then by the Hash codes of target character Value is determined as the Hash code value of string data.

It should be understood that in finding out string data after the Hash code value of target character, for still being deposited in string data Untreated character, then need to continue through Hash processing (concrete processing procedure can be found in the handling principle in Fig. 2), wait for a right man to marry It accords with when untreated character being not present in string data, it at this time can will treated that the corresponding Hash code value of target character is true by Hash It is set to the Hash code value of string data, to complete the Hash evaluation to string data.Since by Hash, treated Hash code value is acquired after mixing and upsetting string data, therefore can reduce the repetition of the Hash code value of generation Rate, the safety of the Hash code value of string data obtained by improving.

Further, the high position that processing unit 506 is also used to will add up treated Hash code value is set as zero, to obtain The target cryptographic Hash of target character.Specifically, processing unit 506 is carried out for the Hash code value that will add up that treated and preset value Position and operation, to obtain the target cryptographic Hash of target character, wherein preset value 0x7fffffff.I.e. by will add up processing The high position zero of Hash code value afterwards, being equivalent to will add up that treated Hash code value is converted to positive value, the target word made The target cryptographic Hash of symbol is positive number, and the cryptographic Hash of string data is determined with the target cryptographic Hash according to target character.

The character string Hash processing unit of the embodiment of the present invention is directed to the Hash calculation demand of string data, uses shifting Position, negate with three kinds of basic operations of addition, devise the hash method of processing character string type data, this method not only handles speed Degree is than very fast, and obtained Hash result distributivity is very good.Applicant have surprisingly discovered that with BKDRHash algorithm is only passed through Scheme compare, the scheme fast 8% or so of the processing speed ratio BKDRHash algorithm of the method for the embodiment of the present invention, wherein right Than testing 26,000,000 character strings, the Hash codes that two hash algorithms calculate do not have repetition.As it can be seen that the word of the embodiment of the present invention Symbol string Hash processing method can not only improve the speed that Hash processing is carried out to string data, additionally it is possible to reduce the Kazakhstan of generation The repetitive rate of uncommon code value, the safety of the Hash code value of string data obtained by improving.

Character string Hash processing unit described in any of the above-described embodiment is configurable to virtual machine, application program, fortune The computer installation etc. of row UI.

Character string Hash processing method or device described in any of the above-described embodiment can be applied to text calculating, data In the application scenarios such as lookup, information checking.

Preferably, the embodiment of the present invention also provides a kind of terminal device, may include processor, and memory is stored in and deposits On reservoir and the computer program that can run on the processor, the computer program realize above-mentioned figure when being executed by processor Each process of embodiment of the method shown in 1-4, and identical technical effect can be reached, to avoid repeating, which is not described herein again.

The embodiment of the present invention also provides a kind of computer readable storage medium, and meter is stored on computer readable storage medium Calculation machine program, the computer program realize each process of method shown in above-mentioned Fig. 1-4 when being executed by processor, and can reach Identical technical effect, to avoid repeating, which is not described herein again.Wherein, the computer readable storage medium is deposited Ru read-only Reservoir (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), magnetic disk or CD etc..

The series of detailed descriptions listed above only for feasible embodiment of the invention specifically Protection scope bright, that they are not intended to limit the invention, it is all without departing from equivalent implementations made by technical spirit of the present invention Or change should all be included in the protection scope of the present invention.

It is obvious to a person skilled in the art that invention is not limited to the details of the above exemplary embodiments, Er Qie In the case where without departing substantially from spirit or essential attributes of the invention, the present invention can be realized in other specific forms.Therefore, no matter From the point of view of which point, the present embodiments are to be considered as illustrative and not restrictive, and the scope of the present invention is by appended power Benefit requires rather than above description limits, it is intended that all by what is fallen within the meaning and scope of the equivalent elements of the claims Variation is included within the present invention.Any reference signs in the claims should not be construed as limiting the involved claims.

In addition, it should be understood that although this specification is described in terms of embodiments, but not each embodiment is only wrapped Containing an independent technical solution, this description of the specification is merely for the sake of clarity, and those skilled in the art should It considers the specification as a whole, the technical solutions in the various embodiments may also be suitably combined, forms those skilled in the art The other embodiments being understood that.

12页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种保护数据隐私的云数据公开验证方法

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!