Best Way to Hash Password in C#


There are different techniques to hashing password string using different algorithm. In .net this can be done very easily using SHA256Managed class. Below is one simple utility function to hash password using SHA256 encryption.

This function return hashed data in Base64 form.

Example to hash password

public static string HashPassword(string plainMessage)
{
    var data = Encoding.UTF8.GetBytes(plainMessage);
    using (HashAlgorithm sha = new SHA256Managed())
    {
        sha.TransformFinalBlock(data, 0, data.Length);
        return Convert.ToBase64String(sha.Hash);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.