DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
PasswordCrypto.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Text;
19 
20 namespace Deveel.Data.Security {
21  public sealed class PasswordCrypto {
22  public PasswordCrypto(string hashName, int keyLength) {
23  KeyLength = keyLength;
24  HashName = hashName;
25 
26  var hash = HashFunctions.GetFunction(hashName);
27  if (hash == null)
28  throw new ArgumentException(String.Format("Hash function {0} is not supported.", hashName));
29  if (!(hash is IKeyedHashFunction))
30  throw new ArgumentException(String.Format("Hash function {0} does not handle keys.", hashName));
31 
32  HashFunction = hash as IKeyedHashFunction;
33  }
34 
35  public string HashName { get; private set; }
36 
37  public int KeyLength { get; private set; }
38 
39  private IKeyedHashFunction HashFunction { get; set; }
40 
41  public string Hash(string password, out string salt) {
42  salt = HashFunction.GenerateSaltString();
43  return HashFunction.MakePbkdf2String(password, salt, 32);
44  }
45 
46  public bool Verify(string hashedPassword, string password, string salt) {
47  return HashFunction.VerifyPbkdf2String(hashedPassword, password, salt);
48  }
49 
50  public override string ToString() {
51  return String.Format("{0}({1})", HashName, KeyLength);
52  }
53 
54  public static PasswordCrypto Parse(string defString) {
55  if (String.IsNullOrEmpty(defString))
56  throw new ArgumentNullException("defString");
57 
58  var sIndex = defString.IndexOf('(');
59  if (sIndex == -1)
60  throw new FormatException();
61 
62  var eIndex = defString.IndexOf(')');
63  if (eIndex == -1)
64  throw new FormatException();
65 
66  var hashName = defString.Substring(0, sIndex);
67  var sKeyLength = defString.Substring(sIndex + 1, eIndex - (sIndex + 1));
68 
69  hashName = hashName.Trim();
70 
71  if (String.IsNullOrEmpty(hashName))
72  throw new FormatException();
73 
74  int keyLength;
75  if (!Int32.TryParse(sKeyLength, out keyLength))
76  throw new FormatException();
77 
78  return new PasswordCrypto(hashName, keyLength);
79  }
80  }
81 }
bool Verify(string hashedPassword, string password, string salt)
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.