我有以下Python代码:SHA1哈希澄清
from hashlib import sha1
secretString=b"this is the secret string"
publicData=b"x10291434"
hash=sha1(publicData+secretString).hexdigest()
,如果我现在大众消费送出去的publicData
和hash
。这安全吗?我想检查一下,当用户提供publicData
时,它与我原来用secretKey
发送的hash
相匹配。
我只是想确认我在做这个正确
你应该使用正确的[HMAC构造](https://en.wikipedia.org/wiki/Hash-based_message_authentication_code) –
Dan D说了些什么。使用标准[hmac](https://docs.python.org/3/library/hmac.html)模块中的任何hashlib散列函数都很容易。对于新代码,您可能会使用更强大的散列,例如SHA-256,但正如维基百科文章中提到的那样,HMAC构造抵抗简单MD5或SHA-1散列可能发生的各种攻击。 –
Insterad使用HMAC,请参阅:[用于消息身份验证的键控哈希](https://docs.python.org/2/library/hmac.html#module-hmac)。 – zaph