HMAC SHA1 对密钥和消息使用相同的值

发布于 2024-12-27 23:12:53 字数 444 浏览 0 评论 0原文

我正在编写一些 C# 安全代码,当我看到它使用 HMACSHA1 类时,我正打算替换它。该代码用于对密码进行哈希处理以存储在数据库中。引起我注意的是它使用密码作为 HMAC 密钥,这正是计算哈希值的目的。那么,使用密钥和散列数据的数据可以吗?这会使安全性更强还是更弱?

伪代码:

string pwd = "My~!Cra2y~P@ssWord1#123$";

using (HMACSHA1 hasher = new HMACSHA1())
{
    hasher.Key = encoding.GetBytes(pwd); // using password for the key
    return BytesToHex(hasher.ComputeHash(encoding.GetBytes(pwd))); // computing the hash for the password
}

I'm working on some C# security code and was about to replace it when I saw that it was using the HMACSHA1 class. The code is used to hash a password for storing in the database. The thing that caught my eye was that it uses the password as the HMAC key which is exactly what is computing the hash for. So is using the data for both the key and the thing your hashing OK? Does this make the security stronger or weaker?

psuedo code:

string pwd = "My~!Cra2y~P@ssWord1#123$";

using (HMACSHA1 hasher = new HMACSHA1())
{
    hasher.Key = encoding.GetBytes(pwd); // using password for the key
    return BytesToHex(hasher.ComputeHash(encoding.GetBytes(pwd))); // computing the hash for the password
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2025-01-03 23:12:53

它的强度与经过两次迭代的未加盐 SHA1 哈希值一样强。即相当弱。

由于缺少盐,攻击者可以创建彩虹表,或者简单地同时攻击数据库中的所有密码哈希值。

低迭代次数使得攻击速度更快,因为攻击者可以简单地尝试更多的候选密码。

您应该添加盐,并使用较慢的哈希方法,例如 PBKDF2 和 bcrypt。 .net 类 Rfc2898DeriveBytes 实现 PBKDF2,因此我建议使用那个。

It's about as strong as an unsalted SHA1 hash with two iterations. i.e. pretty weak.

The lack of salt allows an attack to create rainbow tables, or simply attack all password hashes in your database at the same time.

The low iteration count makes the attack fast, since the attacker can simply try more password candidates.

You should add a salt, and use a slower hashing method, such as PBKDF2 and bcrypt. The .net class Rfc2898DeriveBytes implements PBKDF2, so I recommend using that one.

成熟的代价 2025-01-03 23:12:53

我不推荐使用 HMACSHA1 来存储数据库密码,但是将 Key 设置为与密码相同确实会削弱 Key 在此用途中的用处。该密钥应该是秘密的,并用于确定底层哈希数据是否已更改。

对于密码,您应该使用 SALT+密码组合来提高 HASH 算法的安全性。我通常使用用户唯一的SALT,但与密码不同,例如用户号或初始注册IP地址。

另外,请记住,不再建议将 SHA1 作为哈希算法。

您可以参考MSDN以获得更清晰的信息理解。

此属性是键控哈希算法的密钥。

基于哈希的消息验证代码 (HMAC) 可用于
确定通过不安全通道发送的消息是否已被
被篡改,前提是发送者和接收者共享秘密
钥匙。发送方计算原始数据的哈希值并
将原始数据和 HMAC 作为单个消息发送。这
接收者重新计算接收到的消息的哈希值并检查
计算出的哈希值与传输的哈希值相匹配。

HMAC 可与任何迭代加密哈希函数一起使用,例如
MD5 或 SHA-1 与秘密共享密钥相结合。这
HMAC 的加密强度取决于
底层哈希函数。

对数据或哈希值的任何更改都会导致不匹配,
因为需要知道密钥才能更改消息
并重现正确的哈希值。因此,如果原来的和
计算出的哈希值匹配,消息已通过身份验证。

I wouldn't recommend HMACSHA1 for database password storage, but setting the Key to be the same as the password does weaken the usefulness of the Key in this purpose. The key is supposed to be secret and used to determine if the underlying hashed data has changed.

For passwords you should be using a SALT+Password combination to increase the security of HASH algorithms. I usually use a SALT that is unique to the user, but not the same as the password, such as the user number or initial registration IP address.

Also, keep in mind that SHA1 is no longer recommended as a hashing algorithm.

You can reference MSDN for a clearer understanding.

This property is the key for the keyed hash algorithm.

A Hash-based Message Authentication Code (HMAC) can be used to
determine whether a message sent over an insecure channel has been
tampered with, provided that the sender and receiver share a secret
key. The sender computes the hash value for the original data and
sends both the original data and the HMAC as a single message. The
receiver recomputes the hash value on the received message and checks
that the computed hash value matches the transmitted hash value.

HMAC can be used with any iterative cryptographic hash function, such
as MD5 or SHA-1, in combination with a secret shared key. The
cryptographic strength of HMAC depends on the properties of the
underlying hash function.

Any change to the data or the hash value results in a mismatch,
because knowledge of the secret key is required to change the message
and reproduce the correct hash value. Therefore, if the original and
computed hash values match, the message is authenticated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文