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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more