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 Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

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

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay