【问题标题】:Will an MD5 hash keep changing as its input grows?MD5 哈希值会随着输入的增长而不断变化吗?
【发布时间】:2012-02-22 06:10:29
【问题描述】:

MySQL 的 MD5 散列函数返回的值是否会随着给它的字符串无限增长而继续无限变化?

例如,这些是否会继续返回不同的值:

MD5("A"+"B"+"C")
MD5("A"+"B"+"C"+"D")
MD5("A"+"B"+"C"+"D"+"E")
MD5("A"+"B"+"C"+"D"+"E"+"D")
... and so on until a very long list of values ....

在某些时候,当我们为函数提供很长的输入字符串时,结果是否会停止变化,就像输入被截断一样?

我问是因为我想使用 MD5 函数通过存储这些字段的 MD5 哈希来比较两条记录与大量字段。

======== 虚构示例(您不需要这个来回答问题,但它可能会让您感兴趣:========

我有一个数据库应用程序,它定期从外部来源获取数据并使用它来更新 MySQL 表。

假设在第 1 个月,我进行了第一次下载:

downloaded data, where the first field is an ID, a key:
    1,"A","B","C"
    2,"A","D","E"
    3,"B","D","E"

I store this
    1,"A","B","C"
    2,"A","D","E"
    3,"B","D","E"

第 2 个月,我得到 1,"A","B","C" 2,"A","D","X" 3,"B","D","E" 4,"B","F","E"

Notice that the record with ID 2 has changed.  Record with ID 4 is new.  So I store two new records:
    1,"A","B","C"
    2,"A","D","E"
    3,"B","D","E"
    2,"A","D","X"
    4,"B","F","E"

This way I have a history of *changes* to the data.

I don't want have to compare each field of the incoming data with each field of each of the stored records.
E.g., if I'm comparing incoming record x with exiting record a, I don't want to have to say:
    Add record x to the stored data if there is no record a such that x.ID == a.ID AND x.F1 == a.F1 AND x.F2 == a.F2 AND x.F3 == a.F3 [4 comparisons]

What I want to do is to compute an MD5 hash and store it:
    1,"A","B","C",MD5("A"+"B"+"C")

Let's suppose that it is month #3, and I get a record:
    1,"A","G","C"
What I want to do is compute the MD5 hash of the new fields: MD5("A"+"G"+"C") and compare the resulting hash with the hashes in the stored data.
If it doesn't match, then I add it as a new record.
I.e., Add record x to the stored data if there is no record a such that x.ID == a.ID AND MD5(x.F1 + x.F2 + x.F3) == a.stored_MD5_value [2 comparisons]

My question is "Can I compare the MD5 hash of, say, 50 fields without increasing the likelihood of clashes?"

【问题讨论】:

    标签: mysql md5


    【解决方案1】:

    是的,实际上,它应该不断变化。由于pigeonhole principle,如果你继续这样做,你最终应该会发生碰撞,但你会达到那个点是不切实际的。

    【讨论】:

    • 谢谢 - 这就是我想听到的答案。并且冲突应该不是什么大问题,因为我只会比较具有相同数字标识符的记录。现在我得去写我的代码了。能够带着问题入睡并醒来寻找等待的答案真是太好了!
    【解决方案2】:

    MD5 哈希函数的安全性受到严重威胁。存在碰撞攻击,可以在几秒钟内在具有 2.6Ghz Pentium4 处理器的计算机上发现碰撞(复杂度为 224)。 此外,使用现成的计算硬件(复杂度 239),还有一种选择前缀冲突攻击可以在数小时内为两个选择的任意不同输入产生冲突。 使用现成的 GPU 极大地帮助了发现碰撞的能力。在 NVIDIA GeForce 8400GS 图形处理器上,每秒可以计算 16-18 百万次哈希。 NVIDIA GeForce 8800 Ultra 每秒可以计算超过 2 亿次哈希。

    这些哈希和冲突攻击已在各种情况下公开展示,包括冲突的文档文件和数字证书。 见http://www.win.tue.nl/hashclash/On%20Collisions%20for%20MD5%20-%20M.M.J.%20Stevens.pdf

    许多项目已经在网上发布了 MD5 彩虹表,可用于将许多 MD5 哈希反转为与原始输入冲突的字符串,通常用于密码破解。

    【讨论】:

    • 很高兴您知道并分享这些知识,但请注意,并非所有 MD5 的使用都与安全性有关。喜欢这个。
    • 使用足够长的盐会使 MD5 攻击技术失效。
    • 又对了,但是这个问题中MD5的应用与安全性无关。它永远不会受到攻击,因为没有任何意义。
    • Mchl 是绝对正确的:我根本没有将其用于安全性,只是为了使记录比较更容易。我在 stackoverflow 上提出这个问题的部分原因是,我只想问高质量的问题,因为搜索答案会返回很多关于密码加密而不是算法本身的内容。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多