Dans un effort continu pour améliorer mon code, je cherche le meilleur moyen de créer et de comparer un mot de passe salé dans .NET.Quelle est la manière la plus simple de créer et de comparer un mot de passe salé dans .NET?
Y a-t-il une meilleure façon, ou plus sûre, de faire cela?
Mon code actuel est le suivant:
public static string CreateSaltedPassword(string salt, string password)
{
SHA1CryptoServiceProvider SHA1 = null;
SHA1 = new SHA1CryptoServiceProvider();
// Convert the string into an array of bytes
byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(salt + password);
// Compute the hash value
byte[] byteHash = SHA1.ComputeHash(byteValue);
// Dispose the unmanaged cryptographic object
SHA1.Clear();
return Convert.ToBase64String(byteHash);
}
public static bool ComparePasswords(string salt, string password, string storedPassword)
{
string passwordHash = string.Empty;
// Create the hashed password
passwordHash = PasswordProvider.CreateSaltedPassword(
salt, password);
// Compare the passwords
return (string.Compare(storedPassword, passwordHash) == 0);
}
Vous devriez probablement ajouter quelques informations sur chaque itération effectuée, car simplement répéter la fonction de hachage N fois augmente légèrement le risque de collision. Voir http://en.wikipedia.org/wiki/Key_strengthening#Hash_based_key_stretching – ollb
Oui, il n'est pas suffisant d'itérer un hachage pour plusieurs raisons. Au minimum, PBKDF2 devrait être utilisé pour préserver l'entropie présente dans le mot de passe. – erickson