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
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 RNGCryptoServiceProvider
can no longer work. The concept of a "crypto service provider" typically means it's implemented by CAPI in Windows. Hence it has been replaced.
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
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With