DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Static Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Security.CryptoHashExtenions Class Reference

Static Public Member Functions

static string MakePbkdf2String (this IKeyedHashFunction hash, string input, string salt, int length)
 
static string MakePbkdf2String (this IKeyedHashFunction hash, string input, string salt, int length, int iterationCount)
 
static string MakePbkdf2String (this IKeyedHashFunction hash, string input, byte[] salt, int length)
 
static string MakePbkdf2String (this IKeyedHashFunction hash, string input, byte[] salt, int length, int iterationCount)
 
static byte[] MakePbkdf2 (this IKeyedHashFunction hash, byte[] salt, int length, int iterationCount)
 
static byte[] MakePbkdf2 (this IKeyedHashFunction hash, byte[] data, byte[] salt, int length)
 
static byte[] MakePbkdf2 (this IKeyedHashFunction hash, byte[] data, byte[] salt, int length, int iterationCount)
 
static bool VerifyPbkdf2 (this IKeyedHashFunction hash, byte[] hashed, byte[] otherData, byte[] salt)
 
static bool VerifyPbkdf2 (this IKeyedHashFunction hash, byte[] hashed, byte[] otherData, byte[] salt, int iterationCount)
 
static bool VerifyPbkdf2String (this IKeyedHashFunction hash, string hashedString, string otherString, byte[] salt)
 
static bool VerifyPbkdf2String (this IKeyedHashFunction hash, string hashedString, string otherString, byte[] salt, int iterationCount)
 
static bool VerifyPbkdf2String (this IKeyedHashFunction hash, string hashedString, string otherString, string saltString)
 
static bool VerifyPbkdf2String (this IKeyedHashFunction hash, string hashedString, string otherString, string saltString, int iterationCount)
 
static string ComputeString (this IHashFunction hash, string s)
 
static byte[] GenerateSalt (this IHashFunction hash)
 
static string GenerateSaltString (this IHashFunction hash)
 

Static Private Member Functions

static bool ByteArraysEqual (byte[] a, byte[] b)
 
static string BinaryToHex (byte[] data)
 

Private Attributes

const int DefaultIterationCount = 10000
 

Detailed Description

Definition at line 23 of file CryptoHashExtensions.cs.

Member Function Documentation

static string Deveel.Data.Security.CryptoHashExtenions.BinaryToHex ( byte[]  data)
inlinestaticprivate

Definition at line 182 of file CryptoHashExtensions.cs.

182  {
183  char[] hex = new char[data.Length * 2];
184 
185  for (int iter = 0; iter < data.Length; iter++) {
186  byte hexChar = ((byte)(data[iter] >> 4));
187  hex[iter * 2] = (char)(hexChar > 9 ? hexChar + 0x37 : hexChar + 0x30);
188  hexChar = ((byte)(data[iter] & 0xF));
189  hex[(iter * 2) + 1] = (char)(hexChar > 9 ? hexChar + 0x37 : hexChar + 0x30);
190  }
191  return new string(hex);
192  }
static bool Deveel.Data.Security.CryptoHashExtenions.ByteArraysEqual ( byte[]  a,
byte[]  b 
)
inlinestaticprivate

Definition at line 147 of file CryptoHashExtensions.cs.

147  {
148  if (ReferenceEquals(a, b)) {
149  return true;
150  }
151 
152  if (a == null || b == null || a.Length != b.Length) {
153  return false;
154  }
155 
156  bool areSame = true;
157  for (int i = 0; i < a.Length; i++) {
158  areSame &= (a[i] == b[i]);
159  }
160  return areSame;
161  }
static string Deveel.Data.Security.CryptoHashExtenions.ComputeString ( this IHashFunction  hash,
string  s 
)
inlinestatic

Definition at line 165 of file CryptoHashExtensions.cs.

165  {
166  var data = Encoding.UTF8.GetBytes(s);
167  return BinaryToHex(hash.Compute(data));
168  }
static byte [] Deveel.Data.Security.CryptoHashExtenions.GenerateSalt ( this IHashFunction  hash)
inlinestatic

Definition at line 170 of file CryptoHashExtensions.cs.

170  {
171  int byteLength = (hash.HashSize / 2) / 8;
172  byte[] buf = new byte[byteLength];
173  var rng = new RNGCryptoServiceProvider();
174  rng.GetBytes(buf);
175  return buf;
176  }
static string Deveel.Data.Security.CryptoHashExtenions.GenerateSaltString ( this IHashFunction  hash)
inlinestatic

Definition at line 178 of file CryptoHashExtensions.cs.

178  {
179  return Convert.ToBase64String(hash.GenerateSalt());
180  }
static byte [] Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2 ( this IKeyedHashFunction  hash,
byte[]  salt,
int  length,
int  iterationCount 
)
inlinestatic

Definition at line 46 of file CryptoHashExtensions.cs.

46  {
47  var hashKey = hash.Key;
48  if (hashKey == null || hashKey.Length == 0)
49  throw new InvalidOperationException("Key was not specified for the hash.");
50 
51  return MakePbkdf2(hash, hashKey, salt, length, iterationCount);
52  }
static byte[] MakePbkdf2(this IKeyedHashFunction hash, byte[] salt, int length, int iterationCount)
static byte [] Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2 ( this IKeyedHashFunction  hash,
byte[]  data,
byte[]  salt,
int  length 
)
inlinestatic

Definition at line 54 of file CryptoHashExtensions.cs.

54  {
55  return MakePbkdf2(hash, data, salt, length, DefaultIterationCount);
56  }
static byte[] MakePbkdf2(this IKeyedHashFunction hash, byte[] salt, int length, int iterationCount)
static byte [] Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2 ( this IKeyedHashFunction  hash,
byte[]  data,
byte[]  salt,
int  length,
int  iterationCount 
)
inlinestatic

Definition at line 58 of file CryptoHashExtensions.cs.

58  {
59  hash.Key = data;
60 
61  int hashLength = hash.HashSize/8;
62  if ((hash.HashSize & 7) != 0)
63  hashLength++;
64 
65  int keyLength = length/hashLength;
66  if (length > (0xFFFFFFFFL*hashLength) || length < 0)
67  throw new ArgumentOutOfRangeException("length");
68 
69  if (length%hashLength != 0)
70  keyLength++;
71 
72  byte[] extendedkey = new byte[salt.Length + 4];
73  Buffer.BlockCopy(salt, 0, extendedkey, 0, salt.Length);
74 
75  using (var ms = new MemoryStream()) {
76  for (int i = 0; i < keyLength; i++) {
77  extendedkey[salt.Length] = (byte) (((i + 1) >> 24) & 0xFF);
78  extendedkey[salt.Length + 1] = (byte) (((i + 1) >> 16) & 0xFF);
79  extendedkey[salt.Length + 2] = (byte) (((i + 1) >> 8) & 0xFF);
80  extendedkey[salt.Length + 3] = (byte) (((i + 1)) & 0xFF);
81 
82  byte[] u = hash.Compute(extendedkey);
83  Array.Clear(extendedkey, salt.Length, 4);
84 
85  byte[] f = u;
86  for (int j = 1; j < iterationCount; j++) {
87  u = hash.Compute(u);
88  for (int k = 0; k < f.Length; k++) {
89  f[k] ^= u[k];
90  }
91  }
92 
93  ms.Write(f, 0, f.Length);
94  Array.Clear(u, 0, u.Length);
95  Array.Clear(f, 0, f.Length);
96  }
97 
98  byte[] dk = new byte[length];
99  ms.Position = 0;
100  ms.Read(dk, 0, length);
101  ms.Position = 0;
102 
103  for (long i = 0; i < ms.Length; i++) {
104  ms.WriteByte(0);
105  }
106 
107  Array.Clear(extendedkey, 0, extendedkey.Length);
108  return dk;
109  }
110  }
static string Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2String ( this IKeyedHashFunction  hash,
string  input,
string  salt,
int  length 
)
inlinestatic

Definition at line 28 of file CryptoHashExtensions.cs.

28  {
29  return MakePbkdf2String(hash, input, salt, length, DefaultIterationCount);
30  }
static string MakePbkdf2String(this IKeyedHashFunction hash, string input, string salt, int length)
static string Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2String ( this IKeyedHashFunction  hash,
string  input,
string  salt,
int  length,
int  iterationCount 
)
inlinestatic

Definition at line 32 of file CryptoHashExtensions.cs.

32  {
33  return hash.MakePbkdf2String(input, Convert.FromBase64String(salt), length, iterationCount);
34  }
static string Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2String ( this IKeyedHashFunction  hash,
string  input,
byte[]  salt,
int  length 
)
inlinestatic

Definition at line 36 of file CryptoHashExtensions.cs.

36  {
37  return MakePbkdf2String(hash, input, salt, length, DefaultIterationCount);
38  }
static string MakePbkdf2String(this IKeyedHashFunction hash, string input, string salt, int length)
static string Deveel.Data.Security.CryptoHashExtenions.MakePbkdf2String ( this IKeyedHashFunction  hash,
string  input,
byte[]  salt,
int  length,
int  iterationCount 
)
inlinestatic

Definition at line 40 of file CryptoHashExtensions.cs.

40  {
41  var data = Encoding.UTF8.GetBytes(input);
42  var result = hash.MakePbkdf2(data, salt, length, iterationCount);
43  return Convert.ToBase64String(result);
44  }
static bool Deveel.Data.Security.CryptoHashExtenions.VerifyPbkdf2 ( this IKeyedHashFunction  hash,
byte[]  hashed,
byte[]  otherData,
byte[]  salt 
)
inlinestatic

Definition at line 112 of file CryptoHashExtensions.cs.

112  {
113  return VerifyPbkdf2(hash, hashed, otherData, salt, DefaultIterationCount);
114  }
static bool VerifyPbkdf2(this IKeyedHashFunction hash, byte[] hashed, byte[] otherData, byte[] salt)
static bool Deveel.Data.Security.CryptoHashExtenions.VerifyPbkdf2 ( this IKeyedHashFunction  hash,
byte[]  hashed,
byte[]  otherData,
byte[]  salt,
int  iterationCount 
)
inlinestatic

Definition at line 116 of file CryptoHashExtensions.cs.

116  {
117  int length = hashed.Length;
118 
119  var otherHashed = hash.MakePbkdf2(otherData, salt, length, iterationCount);
120 
121  if (otherHashed.Length != hashed.Length)
122  return false;
123 
124  return ByteArraysEqual(hashed, otherHashed);
125  }
static bool ByteArraysEqual(byte[] a, byte[] b)
static bool Deveel.Data.Security.CryptoHashExtenions.VerifyPbkdf2String ( this IKeyedHashFunction  hash,
string  hashedString,
string  otherString,
byte[]  salt 
)
inlinestatic

Definition at line 127 of file CryptoHashExtensions.cs.

127  {
128  return VerifyPbkdf2String(hash, hashedString, otherString, salt, DefaultIterationCount);
129  }
static bool VerifyPbkdf2String(this IKeyedHashFunction hash, string hashedString, string otherString, byte[] salt)
static bool Deveel.Data.Security.CryptoHashExtenions.VerifyPbkdf2String ( this IKeyedHashFunction  hash,
string  hashedString,
string  otherString,
byte[]  salt,
int  iterationCount 
)
inlinestatic

Definition at line 131 of file CryptoHashExtensions.cs.

131  {
132  return hash.VerifyPbkdf2String(hashedString, otherString, Convert.ToBase64String(salt), iterationCount);
133  }
static bool Deveel.Data.Security.CryptoHashExtenions.VerifyPbkdf2String ( this IKeyedHashFunction  hash,
string  hashedString,
string  otherString,
string  saltString 
)
inlinestatic

Definition at line 135 of file CryptoHashExtensions.cs.

135  {
136  return VerifyPbkdf2String(hash, hashedString, otherString, saltString, DefaultIterationCount);
137  }
static bool VerifyPbkdf2String(this IKeyedHashFunction hash, string hashedString, string otherString, byte[] salt)
static bool Deveel.Data.Security.CryptoHashExtenions.VerifyPbkdf2String ( this IKeyedHashFunction  hash,
string  hashedString,
string  otherString,
string  saltString,
int  iterationCount 
)
inlinestatic

Definition at line 139 of file CryptoHashExtensions.cs.

139  {
140  var hashed = Convert.FromBase64String(hashedString);
141  var otherData = Encoding.UTF8.GetBytes(otherString);
142  var salt = Convert.FromBase64String(saltString);
143 
144  return hash.VerifyPbkdf2(hashed, otherData, salt, iterationCount);
145  }

Member Data Documentation

const int Deveel.Data.Security.CryptoHashExtenions.DefaultIterationCount = 10000
private

Definition at line 26 of file CryptoHashExtensions.cs.


The documentation for this class was generated from the following file: