【发布时间】: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?"
【问题讨论】: