the method below stores the password in two fields, a binary[16]
and another binary[64]
, respectively 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, it is necessary to add the following Nuget:
CryptSharp (Official Version)
Hash functions are not suitable to protect a password, and among all of them, MD5 is the most failed and broken of all.
– Tobias Mesquita
This information I was not aware of, for what reasons the use of MD5 is not advisable in this case?
– Renato
A strong cryptographic algorithm has to meet three premises, not suffer from collisions, not be reversible and be costly (to avoid brute force attacks). MD5 as any Hash is not reversible, but unlike SHA2, collisions can occur, and finally every Hash algorithm is very cheap, you can run it millions of times per second.
– Tobias Mesquita
In my answer, I’m using a
Pbkdf2
, that uses a Salt unico per user, as Hash function the SHA512 and 65535 interactions.– Tobias Mesquita
In practice, it is as if it concatenates the password to salt and performs the hash 65535 times, this process takes about 1 second depending on the processor, so a brute force attack is unfeasible.
– Tobias Mesquita
@Renato http://answall.com/q/2402/101
– Maniero
Living and learning, I have always used MD5, although it is a good solution where I am applying, but I will study the application of the algorithms you mentioned. Thank you very much for the explanation.
– Renato
@Renato, dispo, Hash algorithms are ideas to check the integrity of a file, for example, you pass the hash and a download link, the user downloads the file and generates the hash of the file and compares with your hash, if they are equal is pq the file is not corrupted. and for it to be efficient for this use, it needs to be very fast and lightweight.
– Tobias Mesquita