DEV Community

vblover programmer
vblover programmer

Posted on

C#: Secure Password

SecurePassword Class:

using System; using System.Text; using System.Security.Cryptography; namespace UserAccountSystem.Class { public static class SecurePassword { public static String GetMd5Hash(String input) { MD5 md5Hasher = MD5.Create(); byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length ; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } public static bool verifyMd5Hash(String input, String hash) { String hashOfInput = GetMd5Hash(input); StringComparer comparer= StringComparer.OrdinalIgnoreCase; if (hashOfInput.CompareTo(hash) == 0) { return true; } return false; } } } 
Enter fullscreen mode Exit fullscreen mode

Using for password property to save:

public String Password() { return Class.SecurePassword.GetMd5Hash(this.ConfirmPasswordBox.Value); } 
Enter fullscreen mode Exit fullscreen mode

check for password correction:

if (Class.SecurePassword.verifyMd5Hash(this.PasswordBox.Value,UserAccount.TableAdapter.GetPassword(this.ID)) == false) { this.PasswordBox.Value = ""; this.ConfirmPasswordBox.Value = ""; errorProvider1.SetError(this.PasswordBox, "Wrong Password!"); this.PasswordBox.Focus(); this.PasswordBox.Select(); return; } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)