RNGCryptoServiceProvider'无法在DNX Core 5.0中找到

本文关键字:Core DNX RNGCryptoServiceProvider | 更新日期: 2023-09-27 18:14:18

我有哈希生成器类。这应该工作,但我在Visual Studio 2015中有问题。我得到错误

类型或命名空间名称'RNGCryptoServiceProvider'不能找到(您是否缺少using指令或程序集引用?)

类型或命名空间名称"SHA256Managed"无法找到(您是否缺少using指令或程序集引用?)

问题是这在DNX 4.5.1中存在,但在DNX Core 5.0中不存在

public class HashGenerator : IHashGenerator
{
    public string GenerateHash(string input, String salt)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
        SHA256Managed sha256hashstring = new SHA256Managed();
        byte[] hash = sha256hashstring.ComputeHash(bytes);
        return Convert.ToBase64String(bytes);
    }
    public string CreateSalt(int size)
    {
        var rng = new RNGCryptoServiceProvider();
        var buff = new byte[size];
        rng.GetBytes(buff);
        return Convert.ToBase64String(buff);
    }
}

如何在Visual Studio 2015的两个框架中工作?

我的项目。json文件

 "dependencies": {
    "EntityFramework.Commands": "7.0.0-beta8-15718",
    "4tecture.DataAccess.Common": "1.0.0-*",
    "4tecture.DataAccess.EntityFramework": "1.0.0-*",
    "EntityFramework.Relational": "7.0.0-beta8-15723",
    "EntityFramework.SqlServer": "7.0.0-beta8-15797",
    "Microsoft.Framework.ConfigurationModel": "1.0.0-beta5-11337"
  },
  "frameworks": {
    "dnx451": {
      "dependencies": {
      }
    },
    "dnxcore50": {
      "dependencies": {
        "System.Linq": "4.0.1-beta-23302",
        "System.Runtime.Serialization.Primitives": "4.0.11-beta-23302"
      }
    }

Thanks for help

RNGCryptoServiceProvider'无法在DNX Core 5.0中找到

在DNX Core 5.0中,你需要添加一个对System.Security.Cryptography.RandomNumberGenerator的包引用,并从RNGCryptoServiceProvider切换到RandomNumberGenerator类。

由于DNX核心是跨平台的,RNGCryptoServiceProvider不能再工作了。"加密服务提供商"的概念通常意味着它是由Windows中的CAPI实现的。

我在最后实现的解决方案。

 public class HashGenerator : IHashGenerator
    {
        public string GenerateHash(string input, string salt)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
            var hashAlgoritm = System.Security.Cryptography.MD5.Create();
            bytes = hashAlgoritm.ComputeHash(bytes);
            return Convert.ToBase64String(bytes);
        }
        public string CreateSalt()
        {
            var rng = System.Security.Cryptography.RandomNumberGenerator.Create();
            var buff = new byte[25];
            rng.GetBytes(buff);
            return Convert.ToBase64String(buff);
        }
    }

我不能使用SHA256Managed,我一直有命名空间问题。并且不需要对项目进行任何更改。json文件

尝试将该类的依赖项添加到project.json文件

"dependencies": {
"EntityFramework.Commands": "7.0.0-beta8-15718",
"4tecture.DataAccess.Common": "1.0.0-*",
"4tecture.DataAccess.EntityFramework": "1.0.0-*",
"EntityFramework.Relational": "7.0.0-beta8-15723",
"EntityFramework.SqlServer": "7.0.0-beta8-15797",
"Microsoft.Framework.ConfigurationModel": "1.0.0-beta5-11337"
  },
    "frameworks": {
     "dnx451": {
     "dependencies": {
    }
 },
"dnxcore50": {
  "dependencies": {
    "System.Linq": "4.0.1-beta-23302",
    "System.Runtime.Serialization.Primitives": "4.0.11-beta-23302",
    "System.Security.Cryptography.Algorithms": "4.0.0-beta-23225",
    "System.Security.Cryptography.RandomNumberGenerator": "4.0.0-beta-23225"
  }
}

然后将RNGCryptoServiceProvider类更改为RandomNumberGenerator并使用Create方法,以返回RNGCryptoServiceProvider的实例。

public class HashGenerator : IHashGenerator
{
  public string GenerateHash(string input, String salt)
  {
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
    SHA256Managed sha256hashstring = new SHA256Managed();
    byte[] hash = sha256hashstring.ComputeHash(bytes);
    return Convert.ToBase64String(bytes);
  }
  public string CreateSalt(int size)
  {
    var rng = RandomNumberGenerator.Create();
    var buff = new byte[size];
    rng.GetBytes(buff);
    return Convert.ToBase64String(buff);
  }
}

因为答案是不完整的(OP: I could not use SHA256Managed)我张贴了一个解决方案,如何在DNX Core 5.0中使用SHA256Managed

在DNX 4.5.1

HashAlgorithm hash = new SHA256Managed();

在DNX Core 5.0(也可以与DNX 4.5.1一起使用)

HashAlgorithm hash = SHA256.Create();

同样适用于所有位(384,512..)

,你不需要任何版本依赖包。只要using System.Security.Cryptography就够了。