Hashing string using SHA & C#


There are many techniques to hash and encrypt string in .net . We can do this very easily using HashAlgorithm & SHA256Managed class provided by .net framework.

Example of hashing

/// <summary>
/// Encrypts a string using the SHA256 algorithm.
/// </summary>
/// <param name="plainMessage">
/// The plain Message.
/// </param>
/// <returns>
/// The hash password.
/// </returns>
public static string HashString(string plainMessage)
{
    var data = Encoding.UTF8.GetBytes(plainMessage);
    using (HashAlgorithm sha = new SHA256Managed())
    {
        sha.TransformFinalBlock(data, 0, data.Length);
        return Convert.ToBase64String(sha.Hash);
    }
}