forked from MenaceSan/DotStd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashUtil.cs
252 lines (213 loc) · 9.26 KB
/
HashUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DotStd
{
public class HashXXH64 : HashAlgorithm
{
// Fast 64 bit hash. ulong output.
// like GetKnuthHash
// https://stackoverflow.com/questions/8820399/c-sharp-4-0-how-to-get-64-bit-hash-code-of-given-string
// https://github.com/brandondahler/Data.HashFunction/blob/master/src/System.Data.HashFunction.xxHash/xxHash_Implementation.cs
// TODO
public ulong Value { get; private set; } // output short cut.
public override void Initialize()
{
throw new NotImplementedException();
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
throw new NotImplementedException();
}
protected override byte[] HashFinal()
{
throw new NotImplementedException();
}
}
public class HashUtil
{
// Wrapper for HashAlgorithm for use in crypto .
// Hashes can be used for 1. Id. Avoid Accidental Collision, 2. Security. Avoid intentional collision.
// Crypto hashes are usually on strings (passwords). Actual Hash is on String.ToByteArray().
// If we assume a unique salt per user and >= 128 bits of hash, the best attack left is dictionary/brute force password guessing.
// Length of the hash beyond ~256 bits is no longer useful. we need to make it expensive for the attacker.
// Algorithms: PDFKDF2 = weak to FPGA usage. scrypt newer. control cost of attack.
// NOTE: might wrap byte[] hash output in StringUtil.ToHexStr() or Convert.ToBase64String() for db storage as string ?
private HashAlgorithm _Hasher;
public void Init()
{
_Hasher.Initialize();
}
public const ulong kHashValue1 = 3074457345618258791ul;
public const ulong kHashValue2 = 3074457345618258799ul;
public static ulong GetKnuthHash(string read)
{
// Very fast 64 bit string anti-collision hash of a string.
// like object.GetHashCode() but 64 bit. Not cryptographically secure.
// use 64 bits for lower hash collisions.
// ? Make this faster by doing 8 byte chunks?
if (read == null)
return 0;
ulong hashedValue = kHashValue1;
for (int i = 0; i < read.Length; i++)
{
hashedValue += read[i];
hashedValue *= kHashValue2;
}
return hashedValue;
}
public async Task<byte[]> GetHashStreamAsync(Stream stream)
{
// like HashAlgorithm.ComputeHash(Stream) but async
var buffer = new byte[8192];
int bytesRead;
// compute the hash on 8KiB blocks
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
_Hasher.TransformBlock(buffer, 0, bytesRead, buffer, 0);
_Hasher.TransformFinalBlock(buffer, 0, bytesRead);
return _Hasher.Hash;
}
public byte[] GetHashFile(string filename)
{
// Hash the contents of a file with _Hasher.
// MD5 = Return 128 bits. 16 bytes for contents of a file. Not crypto. just anti-collision.
// use SHA512 for true crypto.
using (var fs = File.OpenRead(filename))
{
// Convert the input string to a byte array and compute the hash.
return _Hasher.ComputeHash(fs);
}
}
public async Task<byte[]> GetHashFileAsync(string filename)
{
using (var fs = File.OpenRead(filename))
{
// Convert the input string to a byte array and compute the hash.
return await GetHashStreamAsync(fs);
}
}
public static void MergeHash(byte[] inp, byte[] bout)
{
// Combine 2 hashes. try not to lose noise/entropy. xor wrapping extra data (or padding output).
int j = 0;
foreach (byte b in inp)
{
bout[j] ^= b;
if (++j >= bout.Length) j = 0; // wrap back to start. wrap extra data back over previous data.
}
if (inp.Length < bout.Length) // pad output.
{
int i = 0;
for (; j < bout.Length; j++) // fill bout
{
bout[j] ^= inp[i];
if (++i >= inp.Length) i = 0; // wrap to fill bout
}
}
}
public static byte[] MakeHashLen(byte[] inp, int lenOut)
{
// Adjust the length of a hash.
// lenOut = return byte[] size.
if (inp.Length == lenOut) // no change required.
return inp; // inp is ok the way it is.
byte[] bout = new byte[lenOut]; // assume 0 init.
MergeHash(inp, bout);
return bout;
}
public byte[] GetHash(string str)
{
// Convert the input string to a byte array and compute the hash.
// Assume any salt has already been added.
return _Hasher.ComputeHash(str.ToByteArray());
}
public byte[] GetHash(string password, string systemsecret, ulong salt, int id)
{
// crypto hashes are on strings (passwords) SecureString
// Compute a hash of (system secret password + password + ulong random salt + id of user).
// cant share lookup attacks across users or pre-compute.
// all attacks are per user.
return GetHash(string.Concat(password, systemsecret, salt.ToString(), id));
}
public byte[] GetHashSized(string password, string systemsecret, ulong salt, int id, int lenOutBin = 16)
{
// Make a arbitrarily sized hash for a password. for db storage.
// lenOutBin = return size.
return HashUtil.MakeHashLen(GetHash(password, systemsecret, salt, id), lenOutBin);
}
public string GetHashBase64(string password, string systemsecret, ulong salt, int id, int lenOutBase64 = 24)
{
// Get Base64 Hash for password. for db storage.
// leOutnBase64 = how big is the output base64 string. for db storage.
int lenOutBin = SerializeUtil.FromBase64Len(lenOutBase64);
return Convert.ToBase64String(GetHashSized(password, systemsecret, salt, id, lenOutBin));
}
public static int GetHashInt(ulong n)
{
return (int)(n ^ (n >> 32)); // Collapse Hash code.
}
public HashUtil(HashAlgorithm hasher)
{
// Wrap a hash algorithm.
_Hasher = hasher;
}
public static HashAlgorithm GetMD5()
{
// faster. less secure. for low collision hashes.
// used for new account sign up + user email.
// MD5 = Return 128 bits. 16 bytes for a base64 string of 24 chars.
// Wrap a MD5 HashAlgorithm.
// NIST recommends SHA-256 or better for passwords.
// https://stackoverflow.com/questions/247304/what-data-type-to-use-for-hashed-password-field-and-what-length
return new MD5CryptoServiceProvider();
}
public static HashAlgorithm GetSHA256()
{
// Secure hash. SHA256
// The SHA256 hash of "Hello World!" is hex "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069".
// NIST recommends SHA-256 or better for passwords.
// SHA256 = Return 256 bits. 32 bytes for a base64 string of 44 chars.
// https://stackoverflow.com/questions/247304/what-data-type-to-use-for-hashed-password-field-and-what-length
return new SHA256CryptoServiceProvider();
}
public static HashAlgorithm GetSHA512()
{
// secure enough for crypto.
// SHA512 = Return 512 bits. 64 bytes for a base64 string of ?? chars.
// assume >= 64 bit random salt is added to this.
return new SHA512CryptoServiceProvider();
}
public const string kMd5 = "md5";
public const string kSha256 = "sha256";
public static HashAlgorithm FindHasherByName(string hashAlgName)
{
// Lookup hasher by name.
// like static HashAlgorithm.Create(string hashName);
// HMACSHA256 ? HMACSHA512 ?
if (hashAlgName.StartsWith(kMd5))
{
return GetMD5();
}
else if (hashAlgName.StartsWith(kSha256))
{
return GetSHA256();
}
else if (hashAlgName.StartsWith("sha384"))
{
// Secure hash.
// SHA384 = Return 384 bits. 48 bytes for a base64 string of ??? chars.
return new SHA384CryptoServiceProvider();
}
else if (hashAlgName.StartsWith("sha512"))
{
// Secure hash.
// SHA512 = Return 512 bits. 64 bytes for a base64 string of ??? chars.
return GetSHA512();
}
return null; // or default ?
}
}
}