【问题标题】:How to calculate sha 512 hash properly in .NET 6如何在 .NET 6 中正确计算 sha 512 哈希
【发布时间】:2021-11-25 10:34:59
【问题描述】:

在来自How can I SHA512 a string in C#? 的 .NET 6 代码中

  var data = Encoding.UTF8.GetBytes("key");
  byte[] hash;
  using (SHA512 shaM = new SHA512Managed())
    hash = shaM.ComputeHash(data);

抛出警告

Warning SYSLIB0021  'SHA512Managed' is obsolete:
'Derived cryptographic types are obsolete.
Use the Create method on the base type instead.'

Visual Studio 2022 不为此提供代码更改。 如何在 .NET 6 中用正确的代码替换此代码?

从 ASP.NET MVC 控制器调用代码。

【问题讨论】:

  • 为什么不按照警告中的建议:SHA512 shaM = SHA512.Create()System.Security.Cryptography.SHA512
  • 谢谢,它消除了警告。您可以将其写为答案

标签: asp.net-mvc cryptography sha sha512 .net-6.0


【解决方案1】:
    public string CreateSHA512(string strData)
    {
        var message = Encoding.UTF8.GetBytes(strData);
        using (var alg = SHA512.Create())
        {
            string hex = "";

            var hashValue = alg.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }
    }

【讨论】:

  • 如果使用var hex = new StringBuilder(); 改进了这个答案呢?
  • 为什么不使用 .NET 5 中引入的 Convert.ToHexString?
【解决方案2】:

在我的情况下,我在 .NET 5 中使用 RNGCryptoServiceProvider,但是当我更新到 .NET 6 时,我得到了同样的警告。在this issue 中阅读后,我更改了我的代码:

public string HashPassword(string plainPassword)
{
    if (string.IsNullOrEmpty(plainPassword))
    {
        throw new ArgumentNullException(nameof(plainPassword));
    }

    var cryptoProvider = new RNGCryptoServiceProvider();
    byte[] salt = new byte[SaltByteSize];
    cryptoProvider.GetBytes(salt);

    byte[] hash = GetPbkdf2Bytes(plainPassword, salt, Pbkdf2Iterations, HashByteSize);

    return $"{Pbkdf2Iterations}:{Convert.ToBase64String(salt)}:{Convert.ToBase64String(hash)}";
}

到这里:

public string HashPassword(string plainPassword)
{
    if (string.IsNullOrEmpty(plainPassword))
    {
        throw new ArgumentNullException(nameof(plainPassword));
    }

    byte[] salt = RandomNumberGenerator.GetBytes(SaltByteSize);
    byte[] hash = GetPbkdf2Bytes(plainPassword, salt, Pbkdf2Iterations, HashByteSize);

    return $"{Pbkdf2Iterations}:{Convert.ToBase64String(salt)}:{Convert.ToBase64String(hash)}";
}

我知道它不是完全相同的类,但它们是相关的。

【讨论】:

    【解决方案3】:

    您也可以根据this link中微软网站的描述, 使用此代码:

    // Disable the warning.
    #pragma warning disable SYSLIB0001
    
    // Code that uses obsolete API.
    //...
    
    // Re-enable the warning.
    #pragma warning restore SYSLIB0001
    

    【讨论】:

      【解决方案4】:

      与 Sike Mullivan 接受的答案相同,但略短:

          public string CreateSHA512(string strData)
          {
              var message = Encoding.UTF8.GetBytes(strData);
              using var alg = SHA512.Create();
      
              var hashValue = alg.ComputeHash(message);
              return hashValue.Aggregate("", (current, x) => current + $"{x:x2}");
          }
      

      或者,或者,单行:

      public string CreateSHA512(string strData) => SHA512.Create().ComputeHash(Encoding.UTF8.GetBytes(strData)).Aggregate("", (current, x) => current + $"{x:x2}");
      

      【讨论】:

      • 单线是不用的。它不会在返回时处理资源。文本变量也未定义
      猜你喜欢
      • 2011-07-04
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多