【问题标题】:Should SHA1CryptoServiceProvider and SHA256Managed return same valueSHA1CryptoServiceProvider 和 SHA256Managed 是否应该返回相同的值
【发布时间】:2021-11-24 22:50:09
【问题描述】:

我看到一些问题表明生成了相同的哈希,但代码如下:

    var password = "TestPassword";

    using (var hash = new SHA1CryptoServiceProvider())
    {
        var hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
        Console.WriteLine( Convert.ToBase64String(hashBytes) );
    }
    using (var managed = new SHA256Managed())
    {
        var hashBytes = managed.ComputeHash(Encoding.UTF8.GetBytes(password));
        Console.WriteLine(Convert.ToBase64String(hashBytes));
    }

输出是:

YlBiWyJt9ihwriOvjT+sB2DXFYg=
e8+diSmPG/rhb6Au1rYZCP0vqN5F3Y4hU6PEcwB2Uyg=

我没有正确使用加密库吗?

【问题讨论】:

  • SHA-1 和 SHA-256(SHA-2 的变体)是不同的哈希函数。
  • Grr...错过了这种差异。好眼力。

标签: c# sha256


【解决方案1】:

SHA1 和 SHA256 是两个不同的哈希函数。 (来源:https://en.wikipedia.org/wiki/Secure_Hash_Algorithms

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 2011-02-07
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多