using System.Security.Cryptography;
public static void Main()
var plainText = "Wellington Nascimento";
var hashMd5 = Hash.GetHash(plainText, EncryptionAlgorithm.HashAlgorithms.MD5);
var hashSha256 = Hash.GetHash(plainText, EncryptionAlgorithm.HashAlgorithms.SHA256);
Console.WriteLine(hashMd5);
Console.WriteLine(hashSha256);
public static class EncryptionAlgorithm
public enum HashAlgorithms : byte
public enum SymmetricAlgorithms : byte
private static HashAlgorithm _algorithm = null;
public static string GetHash(string plainText, EncryptionAlgorithm.HashAlgorithms hashAlgorithm = EncryptionAlgorithm.HashAlgorithms.Default)
SetHashProvider(hashAlgorithm);
byte[] cryptoByte = _algorithm.ComputeHash(ASCIIEncoding.ASCII.GetBytes(plainText));
var strBuilder = new StringBuilder();
for (int i = 0; i < cryptoByte.Length; i++)
strBuilder.Append(cryptoByte[i].ToString("x2"));
return strBuilder.ToString();
public static string GetHash(FileStream fileStream, EncryptionAlgorithm.HashAlgorithms hashAlgorithm = EncryptionAlgorithm.HashAlgorithms.Default)
SetHashProvider(hashAlgorithm);
byte[] cryptoByte = _algorithm.ComputeHash(fileStream);
return Convert.ToBase64String(cryptoByte, 0, cryptoByte.Length);
public static string GetHashWithSalt(string plainText, EncryptionAlgorithm.HashAlgorithms hashAlgorithm = EncryptionAlgorithm.HashAlgorithms.Default)
const string salt = @"|{E6815A70-5C73-47C6-8DBA-58A9B0BEE0F1}";
var saltedText = plainText + salt;
return Hash.GetHash(saltedText, hashAlgorithm);
private static void SetHashProvider(EncryptionAlgorithm.HashAlgorithms hashProvider)
case EncryptionAlgorithm.HashAlgorithms.MD5:
_algorithm = new MD5CryptoServiceProvider();
case EncryptionAlgorithm.HashAlgorithms.SHA1:
_algorithm = new SHA1Managed();
case EncryptionAlgorithm.HashAlgorithms.SHA256:
_algorithm = new SHA256Managed();
case EncryptionAlgorithm.HashAlgorithms.SHA384:
_algorithm = new SHA384Managed();
case EncryptionAlgorithm.HashAlgorithms.Default:
case EncryptionAlgorithm.HashAlgorithms.SHA512:
_algorithm = new SHA512Managed();