Can two different strings generate the same MD5 hash code?

前端 未结 11 1270
借酒劲吻你
借酒劲吻你 2020-11-28 03:16

For each of our binary assets we generate a MD5 hash. This is used to check whether a certain binary asset is already in our application. But is it possible that two differe

11条回答
  •  粉色の甜心
    2020-11-28 04:04

    I realize this is old, but thought I would contribute my solution. There are a 2^128 possible hash combinations. And thus a 2^64 probability of a birthday paradox. While the solution below won't eliminate possibility of collisions, it surely will reduce the risk by a very substantial amount.

    2^64 = 18,446,744,073,709,500,000 possible combinations
    

    What I have done is I put a few hashes together based on the input string to get a much longer resulting string that you consider your hash...

    So my pseudo-code for this is:

    Result = Hash(string) & Hash(Reverse(string)) & Hash(Length(string))
    

    That is to practical improbability of a collision. But if you want to be super paranoid and can't have it happen, and storage space is not an issue (nor is computing cycles)...

    Result = Hash(string) & Hash(Reverse(string)) & Hash(Length(string)) 
             & Hash(Reverse(SpellOutLengthWithWords(Length(string)))) 
             & Hash(Rotate13(string)) Hash(Hash(string)) & Hash(Reverse(Hash(string)))
    

    Okay, not the cleanest solution, but this now gets you a lot more play with how infrequently you will run into a collision. To the point I might assume impossibility in all realistic senses of the term.

    For my sake, I think the possibility of a collision is infrequent enough that I will consider this not "surefire" but so unlikely to happen that it suits the need.

    Now the possible combinations goes up significantly. While you could spend a long time on how many combinations this could get you, I will say in theory it lands you SIGNIFICANTLY more than the quoted number above of

    2^64 (or 18,446,744,073,709,551,616) 
    

    Likely by a hundred more digits or so. The theoretical max this could give you would be

    Possible number of resulting strings:

    528294531135665246352339784916516606518847326036121522127960709026673902556724859474417255887657187894674394993257128678882347559502685537250538978462939576908386683999005084168731517676426441053024232908211188404148028292751561738838396898767036476489538580897737998336

提交回复
热议问题