用于哈希名称,firstName和人生的日期的算法 [英] Which algorithm for hashing name, firstName and birth-date of a person
问题描述
我必须将个人的姓氏,名字和出生日期的组合保存为哈希。此哈希值稍后用于搜索具有完全相同属性的同一个人。
我的问题是,如果SHA-1是一个有意义的算法。
I have to save the combination of lastname, firstname and birth-date of a person as a hash. This hash is later used to search for the same person with the exactly same properties. My question is, if SHA-1 is a meaningfull algorithm for this.
据我理解SHA-1,不同的人(具有不同的属性)将获得相同的哈希值。这是正确的吗?
As far as I understand SHA-1, there is virtually no possibility that two different persons (with different attributes) will ever get the same hash-value. Is this right?
推荐答案
如果你想搜索一个只知道 可以将SHA-1存储在数据库中(或者MD5用于速度,除非你有一个四舍五入的人来抽样)。
If you want to search for a person knowing only those credentials, you could store the SHA-1 in the database(or MD5 for speed, unless you have like a quadrillion people to sample).
将没有价值,因为它不存储有关人的信息,但它可以工作在搜索数据库。你只是想确保三个信息匹配,所以它是安全的,只是连接他们:
The hash will be worthless, as it stores no information about the person, but it can work for searching a database. You just want to make sure that the three pieces of information match, so it would be safe to just concatenate them:
user.hash = SHA1(user.firstName + user.DOB + user.lastName)
您可以检查两个是否匹配:
And when you query, you could check if the two match:
hash = SHA1(query.firstName + query.DOB + query.lastName)
for user in database:
if user.hash == hash:
return user
我把 query.DOB
放在中间,因为第一个和最后一个名字可能会碰撞,就像 JohnDoe Bob
出生在
John DoeBob
的同一天。我不知道数字名称,所以我认为这将停止这样的冲突);
I put query.DOB
in the middle because the first and last name might collide, like if JohnDoe Bob
was born on the same day as John DoeBob
. I'm not aware of numeric names, so I think this will stop collisions like those ;)
但是如果这是一个大数据库,我会尝试MD5。它更快,但有一个碰撞的机会(在你的情况下,我可以保证一个不会发生)。然而,碰撞的机会真的很小。
But if this is a big database, I'd try MD5. It's faster, but there is a chance of a collision (in your case, I can guarantee that one won't occur). The chance of a collision, however, is really small.
碰撞是一个 1/2 ^ 128
发生,即:
1
---------------------------------------------------
340,282,366,920,938,463,463,374,607,431,768,211,456
这小于:
0.0000000000000000000000000000000000000293873 %
确定您不会遇到碰撞;)
I'm pretty sure you're not going to get a collision ;)
这篇关于用于哈希名称,firstName和人生的日期的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!