public static string HashPassword(string password, string saltValue)
{
if (String.IsNullOrEmpty(password)) throw new ArgumentException("Password is null");
var encoding = new UnicodeEncoding();
var hash = new SHA256CryptoServiceProvider();
if (saltValue == null)
{
saltValue = GenerateSaltValue();
}
byte[] binarySaltValue = Convert.FromBase64String(saltValue);
byte[] valueToHash = new byte[SaltValueSize + encoding.GetByteCount(password)];
byte[] binaryPassword = encoding.GetBytes(password);
binarySaltValue.CopyTo(valueToHash, 0);
binaryPassword.CopyTo(valueToHash, SaltValueSize);
byte[] hashValue = hash.ComputeHash(valueToHash);
var hashedPassword = String.Empty;
foreach (byte hexdigit in hashValue)
{
hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
}
return hashedPassword;
}