类型或命名空间名称' RNGCryptoServiceProvider'在DNX Core 5.0中找不到

时间:2015-09-24 13:35:29

标签: c# .net dnx

我有Hash Generator类。这应该工作,但我在Visual Studio 2015中有问题。 我收到错误

  

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

     

类型或命名空间名称' SHA256Managed'找不到(是你吗?   缺少using指令或程序集引用?)

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

<?php
ini_set("display_errors", 1);
$systemName = 'yourSystemName';
$userID = 'yourUserID';
$password = 'yourPassword';
$options['i5_libl'] = implode('Z', array(
    'INVALID',        
    'LIB',       
    'LIST',   
    'IMPLODED',  
    'WITH',   
    'THE',      
    'LETTER',       
    'Z'
));
$options['i5_naming'] = DB2_I5_NAMING_ON;

$conn = db2_connect($systemName,$userID,$password,$options);
//The error output to the screen at this point from `ini_set("display_errors", 1);` is:
//Warning: db2_connect(): Statement Execute Failed in /PATH/TO/FILE/test.php on line 58 

echo "<br />|".db2_conn_error()." ||| ".db2_conn_errormsg()."|<br />"; //This displays as "| ||| |"
print_r($conn); //This prints out a resource ID
echo "<br />";

if(isset($conn) && $conn === true){ 
    //Expected to not pass since either false or a resource ID is supposed to be returned.
    //Evaluated to false.
    echo "Boolean true<br />";
}
if(isset($conn) && $conn == true){ 
    //Despite the connection failing according to `ini_set("display_errors", 1)` and a resource ID being reportedly returned
    //this evaluates to true and "Non-Boolean true 2" echos out to the screen.
    echo "Non-Boolean true 2<br />";  
}
if(isset($conn) && $conn == "true"){ 
    //Evaluates to false. db2_connect() returns a resource ID on success, so I did not expect this to evaluate to true.
    echo "String true";
}
if(isset($conn) && $conn === false){ 
    //Expected for this to evaluate to true since an error was logged by ini_set("display_errors", 1)
    //This did not evaluate to true.
    echo "Boolean false<br />";
}
if(isset($conn) && $conn == false){ 
    //Just checking to see if a non-Boolean false was returned.  Evaluates to false.
    echo "Non-Boolean false 2<br />";
}
if(isset($conn) && $conn == "false"){ 
    //Just checking to see if the string "false" was returned.  Evaluates to false.
    echo "String false";
}
?>

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

我的project.json文件

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);
    }
}

感谢您的帮助

4 个答案:

答案 0 :(得分:9)

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

由于DNX Core意味着跨平台,RNGCryptoServiceProvider无法再运行。 &#34;加密服务提供商的概念&#34;通常意味着它是由Windows中的CAPI实现的。因此它已被取代。

答案 1 :(得分:8)

我最后实施的解决方案。

 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,我一直有名称空间问题。并且它不需要在project.json文件中进行任何更改

答案 2 :(得分:3)

尝试将this class的依赖项添加到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);
  }
}

答案 3 :(得分:1)

由于答案不完整(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即可。