using System.Security.Cryptography;
private string GetHash(string text, string passwordFormat)
{
HashAlgorithm _algorithm;
switch (passwordFormat.ToUpper())
{
case "MD5":
_algorithm = MD5.Create();
break;
case "SHA1":
_algorithm = SHA1.Create();
break;
case "SHA256":
_algorithm = SHA256.Create();
break;
case "SHA512":
_algorithm = SHA512.Create();
break;
default:
throw new ArgumentException("Invalid password format.", "passwordFormat");
}
byte[] bytes = Encoding.UTF8.GetBytes(text);
byte[] hash = _algorithm.ComputeHash(bytes);
string hashString = string.Empty;
foreach (byte x in hash)
{
hashString += String.Format("{0:x2}", x);
}
return hashString.ToLowerInvariant();
}
private string GetHash(string text, string passwordFormat)
{
HashAlgorithm _algorithm;
switch (passwordFormat.ToUpper())
{
case "MD5":
_algorithm = MD5.Create();
break;
case "SHA1":
_algorithm = SHA1.Create();
break;
case "SHA256":
_algorithm = SHA256.Create();
break;
case "SHA512":
_algorithm = SHA512.Create();
break;
default:
throw new ArgumentException("Invalid password format.", "passwordFormat");
}
byte[] bytes = Encoding.UTF8.GetBytes(text);
byte[] hash = _algorithm.ComputeHash(bytes);
string hashString = string.Empty;
foreach (byte x in hash)
{
hashString += String.Format("{0:x2}", x);
}
return hashString.ToLowerInvariant();
}