0

I had created a WinForms application with a login system in vs 2019, and now I want to hash the password entered by the user before storing it in the database. But it will exist three errors :

Code:

public static string getHash(string source)
    {
        using (SHA256 sha256Hash = SHA256.Create())
        {
            string hash = getsha256Hash(sha256Hash, source);
            return getsha256Hash(sha256Hash, source);
        }
    }
Peter
  • 19
  • 3
  • https://stackoverflow.com/questions/16999361/obtain-sha-256-string-of-a-string/17001289#17001289 – Dmitry Bychenko Apr 24 '22 at 18:52
  • `using System.Security.Cryptography;` https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha256?view=net-6.0 – Dmitry Bychenko Apr 24 '22 at 18:58
  • 1
    "But it will exist three errors" - what errors? Why are you computing the hash twice? And what does the `getsha256Hash` method look like? Please read https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ for a guide on how to write good Stack Overflow questions - and I'd strong advise you to start following normal .NET naming conventions. – Jon Skeet Dec 22 '22 at 07:43

1 Answers1

0
using System.Security.Cryptography;
using System.Text;
    
private static string GenerateHash(string toHash)
{
    var crypt = new SHA256Managed();
    string hash = String.Empty;
    byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(toHash));
    foreach (byte theByte in crypto)
    {
        hash += theByte.ToString("x2");
    }
    return hash;
}

I once solved this problem like this)).