Raskolnikov
Raskolnikov

Reputation: 3999

The type or namespace name 'RNGCryptoServiceProvider' could not be found in DNX Core 5.0

I have Hash Generator class. This should work, but I have problem in Visual Studio 2015. I get errors

The type or namespace name 'RNGCryptoServiceProvider' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'SHA256Managed' could not be found (are you missing a using directive or an assembly reference?)

Problem is that this exist in DNX 4.5.1, but not in 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);
    }
}

How to make this work in both frameworks in Visual Studio 2015?

My project.json file

 "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

Upvotes: 8

Views: 5822

Answers (4)

Ja9ad335h
Ja9ad335h

Reputation: 5075

since answer is incomplete (OP: I could not use SHA256Managed) i'm posting a solution for how to use SHA256Managed in DNX Core 5.0

in DNX 4.5.1

HashAlgorithm hash = new SHA256Managed();

in DNX Core 5.0 (which also works with DNX 4.5.1)

HashAlgorithm hash = SHA256.Create();

same applies for all bits (384, 512..)

and you dont need any version dependent packages. just using System.Security.Cryptography is enough.

Upvotes: 1

Raskolnikov
Raskolnikov

Reputation: 3999

Solution I implemented at the end.

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

I could not use SHA256Managed, i had namespace issue all the time. And it does not require any changes in project.json file

Upvotes: 8

Overmachine
Overmachine

Reputation: 1733

Try adding the dependency for this class to the project.json file

"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"
  }
}

and then changing the RNGCryptoServiceProvider class to RandomNumberGenerator and using the Create method, to return an instance of 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);
  }
}

Upvotes: 3

vcsjones
vcsjones

Reputation: 141668

In DNX Core 5.0 you need to add a package reference to System.Security.Cryptography.RandomNumberGenerator and switch from RNGCryptoServiceProvider to the RandomNumberGenerator class.

Since DNX Core is meant to be cross platform, the RNGCryptoServiceProvidercan no longer work. The concept of a "crypto service provider" typically means it's implemented by CAPI in Windows. Hence it has been replaced.

Upvotes: 10

Related Questions