using System.Security.Cryptography;
public static class Program
string plaintext = "1qaz2wsx";
byte[] salt = DuotifyHashAlgorithm.GetSalt("KltHKT0bVJZDKYytWsfq924i7fFf883uJM1bXXE09JghRA==");
DuotifyHashAlgorithm.ComputeHash(plaintext, "MD5", salt);
string passwordHashSha1 =
DuotifyHashAlgorithm.ComputeHash(plaintext, "SHA1", salt);
string passwordHashSha256 =
DuotifyHashAlgorithm.ComputeHash(plaintext, "SHA256", salt);
string passwordHashSha384 =
DuotifyHashAlgorithm.ComputeHash(plaintext, "SHA384", salt);
string passwordHashSha512 =
DuotifyHashAlgorithm.ComputeHash(plaintext, "SHA512", salt);
Console.WriteLine("Original String : {0}", plaintext);
Console.WriteLine("Salt value : " + Convert.ToBase64String(salt));
Console.WriteLine("Hash values :\r\n");
Console.WriteLine("MD5 : {0}", passwordHashMD5);
Console.WriteLine("SHA1 : {0}", passwordHashSha1);
Console.WriteLine("SHA256: {0}", passwordHashSha256);
Console.WriteLine("SHA384: {0}", passwordHashSha384);
Console.WriteLine("SHA512: {0}", passwordHashSha512);
public class DuotifyHashAlgorithm
public static byte[] GetSalt(string salt = null)
if (!String.IsNullOrEmpty(salt))
return Convert.FromBase64String(salt);
Random random = new Random();
int saltSize = random.Next(minSaltSize, maxSaltSize);
var saltBytes = new byte[saltSize];
RandomNumberGenerator.Fill(saltBytes);
public static string ComputeHash(string plainText,
throw new ArgumentException("The saltBytes can't be null.", "saltBytes");
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] plainTextWithSaltBytes =
new byte[plainTextBytes.Length + saltBytes.Length];
for (int i = 0; i < plainTextBytes.Length; i++)
plainTextWithSaltBytes[i] = plainTextBytes[i];
for (int i = 0; i < saltBytes.Length; i++)
plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
if (hashAlgorithm == null)
switch (hashAlgorithm.ToUpper())
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
byte[] hashWithSaltBytes = new byte[hashBytes.Length +
string hashValue = Convert.ToBase64String(hashBytes);