DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
HashFunctions.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.Security.Cryptography;
19 
20 namespace Deveel.Data.Security {
21  public static class HashFunctions {
22  public static IHashFunction Sha1 {
23  get { return new HashFunction("SHA1"); }
24  }
25 
26  public static IHashFunction Sha256 {
27  get { return new HashFunction("SHA256"); }
28  }
29 
30  public static IHashFunction Sha384 {
31  get { return new HashFunction("SHA384"); }
32  }
33 
34  public static IHashFunction Sha512 {
35  get { return new HashFunction("SHA512"); }
36  }
37 
38  public static IHashFunction Md5 {
39  get { return new HashFunction("MD5"); }
40  }
41 
42  public static IKeyedHashFunction HmacSha256 {
43  get { return new KeyedHashFunction("HMACSHA256"); }
44  }
45 
46  public static IKeyedHashFunction HmacSha384 {
47  get { return new KeyedHashFunction("HMACSHA384"); }
48  }
49 
50  public static IKeyedHashFunction HmacSha512 {
51  get { return new KeyedHashFunction("HMACSHA512"); }
52  }
53 
54  public static IKeyedHashFunction HmacSha1 {
55  get { return new KeyedHashFunction("HMACSHA1");}
56  }
57 
58  public static IKeyedHashFunction HmacMd5 {
59  get { return new KeyedHashFunction("HMACMD5"); }
60  }
61 
62  public static IHashFunction GetFunction(string functionName) {
63  if (String.Equals(functionName, "SHA256", StringComparison.OrdinalIgnoreCase) ||
64  String.Equals(functionName, "SHA-256", StringComparison.OrdinalIgnoreCase))
65  return Sha256;
66 
67  if (String.Equals(functionName, "SHA384", StringComparison.OrdinalIgnoreCase) ||
68  String.Equals(functionName, "SHA-384", StringComparison.OrdinalIgnoreCase))
69  return Sha384;
70 
71  if (String.Equals(functionName, "SHA512", StringComparison.OrdinalIgnoreCase) ||
72  String.Equals(functionName, "SHA-512", StringComparison.OrdinalIgnoreCase))
73  return Sha512;
74 
75  if (String.Equals(functionName, "MD5", StringComparison.OrdinalIgnoreCase))
76  return Md5;
77 
78  if (String.Equals(functionName, "SHA1", StringComparison.OrdinalIgnoreCase) ||
79  String.Equals(functionName, "SHA-1", StringComparison.OrdinalIgnoreCase))
80  return Sha1;
81 
82  // HMAC
83  if (String.Equals(functionName, "HMAC-SHA256", StringComparison.OrdinalIgnoreCase) ||
84  String.Equals(functionName, "HMACSHA256", StringComparison.OrdinalIgnoreCase) ||
85  String.Equals(functionName, "HMAC-SHA-256", StringComparison.OrdinalIgnoreCase))
86  return HmacSha256;
87 
88  if (String.Equals(functionName, "HMAC-SHA384", StringComparison.OrdinalIgnoreCase) ||
89  String.Equals(functionName, "HMACSHA384", StringComparison.OrdinalIgnoreCase) ||
90  String.Equals(functionName, "HMAC-SHA-384", StringComparison.OrdinalIgnoreCase))
91  return HmacSha384;
92 
93  if (String.Equals(functionName, "HMAC-SHA512", StringComparison.OrdinalIgnoreCase) ||
94  String.Equals(functionName, "HMACSHA512", StringComparison.OrdinalIgnoreCase) ||
95  String.Equals(functionName, "HMAC-SHA-512", StringComparison.OrdinalIgnoreCase))
96  return HmacSha512;
97 
98  if (String.Equals(functionName, "HMAC-SHA1", StringComparison.OrdinalIgnoreCase) ||
99  String.Equals(functionName, "HMACSHA1", StringComparison.OrdinalIgnoreCase) ||
100  String.Equals(functionName, "HMAC-SHA-1", StringComparison.OrdinalIgnoreCase))
101  return HmacSha1;
102 
103  if (String.Equals(functionName, "HMAC-MD5", StringComparison.OrdinalIgnoreCase) ||
104  String.Equals(functionName, "HMACMD5", StringComparison.OrdinalIgnoreCase))
105  return HmacMd5;
106 
107  return null;
108  }
109 
110  #region HashFunction
111 
113  public HashFunction(string hashName) {
114  Hash = HashAlgorithm.Create(hashName);
115 
116  if (Hash == null)
117  throw new ArgumentException(String.Format("Hash function {0} is not supported", hashName));
118  }
119 
120  private HashAlgorithm Hash { get; set; }
121 
122  public void Dispose() {
123  Hash.Clear();
124  }
125 
126  public int HashSize {
127  get { return Hash.HashSize; }
128  }
129 
130  public byte[] Compute(byte[] data) {
131  return Hash.ComputeHash(data);
132  }
133  }
134 
135  #endregion
136 
137  #region KeyedHashFunction
138 
140  public KeyedHashFunction(string hashName) {
141  Hash = KeyedHashAlgorithm.Create(hashName);
142 
143  if (Hash == null)
144  throw new ArgumentException(String.Format("The hash function {0} is not supported", hashName));
145  }
146 
147  private KeyedHashAlgorithm Hash { get; set; }
148 
149  public void Dispose() {
150  Hash.Clear();
151  }
152 
153  public int HashSize {
154  get { return Hash.HashSize; }
155  }
156 
157  public byte[] Compute(byte[] data) {
158  return Hash.ComputeHash(data);
159  }
160 
161  public byte[] Key {
162  get { return Hash.Key; }
163  set { Hash.Key = value; }
164  }
165  }
166 
167  #endregion
168  }
169 }
byte[] Compute(byte[] data)
Computes the hash from the given input.
The hash of a password obtained from the configured hash mechanism, given an input password from the ...
byte[] Compute(byte[] data)
Computes the hash from the given input.
Defines a function to hash as user provided password
static IHashFunction GetFunction(string functionName)
An hash function that requires a private key to compute the final result.