20 namespace Deveel.Data.Security {
23 KeyLength = keyLength;
28 throw new ArgumentException(String.Format(
"Hash function {0} is not supported.", hashName));
30 throw new ArgumentException(String.Format(
"Hash function {0} does not handle keys.", hashName));
32 HashFunction = hash as IKeyedHashFunction;
35 public string HashName {
get;
private set; }
37 public int KeyLength {
get;
private set; }
41 public string Hash(
string password, out
string salt) {
42 salt = HashFunction.GenerateSaltString();
43 return HashFunction.MakePbkdf2String(password, salt, 32);
46 public bool Verify(
string hashedPassword,
string password,
string salt) {
47 return HashFunction.VerifyPbkdf2String(hashedPassword, password, salt);
51 return String.Format(
"{0}({1})", HashName, KeyLength);
55 if (String.IsNullOrEmpty(defString))
56 throw new ArgumentNullException(
"defString");
58 var sIndex = defString.IndexOf(
'(');
60 throw new FormatException();
62 var eIndex = defString.IndexOf(
')');
64 throw new FormatException();
66 var hashName = defString.Substring(0, sIndex);
67 var sKeyLength = defString.Substring(sIndex + 1, eIndex - (sIndex + 1));
69 hashName = hashName.Trim();
71 if (String.IsNullOrEmpty(hashName))
72 throw new FormatException();
75 if (!Int32.TryParse(sKeyLength, out keyLength))
76 throw new FormatException();
bool Verify(string hashedPassword, string password, string salt)
override string ToString()
string Hash(string password, out string salt)
PasswordCrypto(string hashName, int keyLength)
static PasswordCrypto Parse(string defString)
static IHashFunction GetFunction(string functionName)
An hash function that requires a private key to compute the final result.