Generate password hash and save to database [closed]

-1

Can someone give me code that addresses how to create a password hash and save it to the database? It's that I tried to do it, but it's giving a headache, in case someone has already done it, I would greatly appreciate it if it helped me in this sense.

    
asked by anonymous 11.07.2016 / 09:39

3 answers

2

The method below stores the password in two fields, a binary[16] and a binary[64] , respectively the salt and the password itself.

private byte[] CreateSalt()
{
    var salt = new byte[16];
    using (var provider = new System.Security.Cryptography.RNGCryptoServiceProvider())
    {
        provider.GetBytes(salt);
    }
    return salt;
}

public async void SalvarSenha(dynamic dto)
{
    var temp = new System.Security.Cryptography.HMACSHA512() { Key = Encoding.UTF8.GetBytes(dto.Password) };
    var salt = this.CreateSalt();
    var password = Pbkdf2.ComputeDerivedKey(temp, salt, UInt16.MaxValue, temp.HashSize / 8);
}

To make the above code work, you need to add the following Nuget:

CryptSharp (Official Version)

    
12.07.2016 / 22:04
-1

This code generates HASH MD5 of passwords when I need to save them in the encrypted database:

public string HashMd5(string input)
{
    MD5 md5Hash = MD5.Create();

    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

    StringBuilder sBuilder = new StringBuilder();

    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }

    return sBuilder.ToString();
}

I have been using it for quite some time now and it has always worked well. I was able to here

    
12.07.2016 / 22:03
-2

Creating a simple hash is not a seven-headed animal. You can elaborate one with a simple logic, just search. Or simply pick up the string typed as a password and call the GetHashCode () extension method to insert and validate user input into the system.

If you want something more advanced and a web project, I recommend implementing Membership or Identity.

    
12.07.2016 / 21:41