[ACCEPTED]-How to calculate sha 512 hash properly in .NET 6-.net-6.0
Accepted answer
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;
}
}
0
you can use this method
public string GetSha256Hash(string input)
{
using (var hashAlgorithm = SHA512.Create())
{
var byteValue = Encoding.UTF8.GetBytes(input);
var byteHash = hashAlgorithm.ComputeHash(byteValue);
return Convert.ToBase64String(byteHash);
}
}
0
In my case I was using RNGCryptoServiceProvider 5 in .NET 5 but when I updated to .NET 6 I 4 got the same warning. After reading about 3 it in this issue I changed my code from this:
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)}";
}
To this:
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)}";
}
I 2 know it's not exactly the same class but 1 they are related.
You can also based on the description of 1 Microsoft website in this link, use this code:
// Disable the warning.
#pragma warning disable SYSLIB0001
// Code that uses obsolete API.
//...
// Re-enable the warning.
#pragma warning restore SYSLIB0001
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.