-
Notifications
You must be signed in to change notification settings - Fork 7
/
Argon2.Helpers.cs
executable file
·381 lines (365 loc) · 14.6 KB
/
Argon2.Helpers.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// <copyright file="Argon2.Helpers.cs" company="Isopoh">
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// </copyright>
namespace Isopoh.Cryptography.Argon2
{
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using SecureArray;
/// <summary>
/// Argon2 Hashing of passwords
/// </summary>
public sealed partial class Argon2
{
/// <summary>
/// Hash the given password to a Argon2 hash string.
/// </summary>
/// <param name="configToHash">
/// Contains all the information used to create the hash returned.
/// </param>
/// <returns>
/// The Argon2 hash of the given password.
/// </returns>
public static string Hash(Argon2Config configToHash)
{
var argon2 = new Argon2(configToHash);
using (var hash = argon2.Hash())
{
return argon2.config.EncodeString(hash.Buffer);
}
}
/// <summary>
/// Hash the given password to a Argon2 hash string.
/// </summary>
/// <param name="password">
/// The password to hash. Gets UTF-8 encoded before hashing.
/// </param>
/// <param name="secret">
/// The secret to use in creating the hash. UTF-8 encoded before hashing. May be null. A
/// <see cref="string"/>.<see cref="string.Empty"/> is treated the same as null.
/// </param>
/// <param name="timeCost">
/// The time cost to use. Defaults to 3.
/// </param>
/// <param name="memoryCost">
/// The memory cost to use. Defaults to 65536 (64K).
/// </param>
/// <param name="parallelism">
/// The parallelism to use. Default to 1 (single threaded).
/// </param>
/// <param name="type">
/// Data-dependent or data-independent. Defaults to data-independent
/// (as recommended for password hashing).
/// </param>
/// <param name="hashLength">
/// The length of the hash in bytes. Note, the string returned base-64
/// encodes this with other parameters so the resulting string is
/// significantly longer.
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// The Argon2 hash of the given password.
/// </returns>
public static string Hash(
byte[] password,
byte[] secret,
int timeCost = 3,
int memoryCost = 65536,
int parallelism = 1,
Argon2Type type = Argon2Type.DataIndependentAddressing,
int hashLength = 32,
SecureArrayCall secureArrayCall = null)
{
byte[] salt = new byte[16];
RandomNumberGenerator.Create().GetBytes(salt);
return Hash(
new Argon2Config
{
TimeCost = timeCost,
MemoryCost = memoryCost,
Threads = parallelism,
Lanes = parallelism,
Password = password,
Secret = secret,
Salt = salt,
HashLength = hashLength,
Version = Argon2Version.Nineteen
});
}
/// <summary>
/// Hash the given password to a Argon2 hash string.
/// </summary>
/// <param name="password">
/// The password to hash. Gets UTF-8 encoded before hashing.
/// </param>
/// <param name="secret">
/// The secret to use in creating the hash. UTF-8 encoded before hashing. May be null. A
/// <see cref="string"/>.<see cref="string.Empty"/> is treated the same as null.
/// </param>
/// <param name="timeCost">
/// The time cost to use. Defaults to 3.
/// </param>
/// <param name="memoryCost">
/// The memory cost to use. Defaults to 65536 (64K).
/// </param>
/// <param name="parallelism">
/// The parallelism to use. Default to 1 (single threaded).
/// </param>
/// <param name="type">
/// Data-dependent or data-independent. Defaults to data-independent
/// (as recommended for password hashing).
/// </param>
/// <param name="hashLength">
/// The length of the hash in bytes. Note, the string returned base-64
/// encodes this with other parameters so the resulting string is
/// significantly longer.
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// The Argon2 hash of the given password.
/// </returns>
public static string Hash(
string password,
string secret,
int timeCost = 3,
int memoryCost = 65536,
int parallelism = 1,
Argon2Type type = Argon2Type.DataIndependentAddressing,
int hashLength = 32,
SecureArrayCall secureArrayCall = null)
{
var secretBuf = string.IsNullOrEmpty(secret)
? null
: SecureArray<byte>.Best(Encoding.UTF8.GetByteCount(secret), secureArrayCall);
try
{
if (secretBuf != null)
{
Encoding.UTF8.GetBytes(secret, 0, secret.Length, secretBuf.Buffer, 0);
}
using (var passwordBuf = SecureArray<byte>.Best(Encoding.UTF8.GetByteCount(password), secureArrayCall))
{
Encoding.UTF8.GetBytes(password, 0, password.Length, passwordBuf.Buffer, 0);
return Hash(
passwordBuf.Buffer,
secretBuf?.Buffer,
timeCost,
memoryCost,
parallelism,
type,
hashLength,
secureArrayCall);
}
}
finally
{
secretBuf?.Dispose();
}
}
/// <summary>
/// Hash the given password to a Argon2 hash string.
/// </summary>
/// <param name="password">
/// The password to hash. Gets UTF-8 encoded before hashing.
/// </param>
/// <param name="timeCost">
/// The time cost to use. Defaults to 3.
/// </param>
/// <param name="memoryCost">
/// The memory cost to use. Defaults to 65536 (64K).
/// </param>
/// <param name="parallelism">
/// The parallelism to use. Default to 1 (single threaded).
/// </param>
/// <param name="type">
/// Data-dependent or data-independent. Defaults to data-independent
/// (as recommended for password hashing).
/// </param>
/// <param name="hashLength">
/// The length of the hash in bytes. Note, the string returned base-64
/// encodes this with other parameters so the resulting string is
/// significantly longer.
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// The Argon2 hash of the given password.
/// </returns>
public static string Hash(
string password,
int timeCost = 3,
int memoryCost = 65536,
int parallelism = 1,
Argon2Type type = Argon2Type.DataIndependentAddressing,
int hashLength = 32,
SecureArrayCall secureArrayCall = null)
{
return Hash(password, null, timeCost, memoryCost, parallelism, type, hashLength, secureArrayCall);
}
/// <summary>
/// Verify the given Argon2 hash as being that of the given password.
/// </summary>
/// <param name="encoded">
/// The Argon2 hash string. This has the actual hash along with other parameters used in the hash.
/// </param>
/// <param name="configToVerify">
/// The configuration that contains the values used to created <paramref name="encoded"/>.
/// </param>
/// <returns>
/// True on success; false otherwise.
/// </returns>
public static bool Verify(
string encoded,
Argon2Config configToVerify)
{
SecureArray<byte> hash = null;
try
{
if (!configToVerify.DecodeString(encoded, out hash) || hash == null)
{
return false;
}
using (var hasherToVerify = new Argon2(configToVerify))
{
using (var hashToVerify = hasherToVerify.Hash())
{
return !hash.Buffer.Where((b, i) => b != hashToVerify[i]).Any();
}
}
}
finally
{
hash?.Dispose();
}
}
/// <summary>
/// Verify the given Argon2 hash as being that of the given password.
/// </summary>
/// <param name="encoded">
/// The Argon2 hash string. This has the actual hash along with other parameters used in the hash.
/// </param>
/// <param name="password">
/// The password to verify
/// </param>
/// <param name="secret">
/// The secret hashed into the password.
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// True on success; false otherwise.
/// </returns>
public static bool Verify(
string encoded,
byte[] password,
byte[] secret,
SecureArrayCall secureArrayCall = null)
{
var configToVerify = new Argon2Config
{
Password = password,
Secret = secret,
SecureArrayCall = secureArrayCall ?? SecureArray.DefaultCall,
};
return Verify(encoded, configToVerify);
}
/// <summary>
/// Verify the given Argon2 hash as being that of the given password.
/// </summary>
/// <param name="encoded">
/// The Argon2 hash string. This has the actual hash along with other parameters used in the hash.
/// </param>
/// <param name="password">
/// The password to verify
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// True on success; false otherwise.
/// </returns>
public static bool Verify(
string encoded,
byte[] password,
SecureArrayCall secureArrayCall = null)
{
return Verify(encoded, password, null, secureArrayCall);
}
/// <summary>
/// Verify the given Argon2 hash as being that of the given password.
/// </summary>
/// <param name="encoded">
/// The Argon2 hash string. This has the actual hash along with other parameters used in the hash.
/// </param>
/// <param name="password">
/// The password to verify. This gets UTF-8 encoded.
/// </param>
/// <param name="secret">
/// The secret used in the creation of <paramref name="encoded"/>. UTF-8 encoded to create the byte-buffer actually used in the verification.
/// May be null for no secret. <see cref="string"/>.<see cref="string.Empty"/> is treated as null.
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// True on success; false otherwise.
/// </returns>
public static bool Verify(
string encoded,
string password,
string secret,
SecureArrayCall secureArrayCall = null)
{
var secretBuf = string.IsNullOrEmpty(secret)
? null
: SecureArray<byte>.Best(Encoding.UTF8.GetByteCount(secret), secureArrayCall);
try
{
if (secretBuf != null)
{
Encoding.UTF8.GetBytes(secret, 0, secret.Length, secretBuf.Buffer, 0);
}
using (var passwordBuf = SecureArray<byte>.Best(Encoding.UTF8.GetByteCount(password), secureArrayCall))
{
Encoding.UTF8.GetBytes(password, 0, password.Length, passwordBuf.Buffer, 0);
return Verify(encoded, passwordBuf.Buffer, secretBuf?.Buffer, secureArrayCall);
}
}
finally
{
secretBuf?.Dispose();
}
}
/// <summary>
/// Verify the given Argon2 hash as being that of the given password.
/// </summary>
/// <param name="encoded">
/// The Argon2 hash string. This has the actual hash along with other parameters used in the hash.
/// </param>
/// <param name="password">
/// The password to verify. This gets UTF-8 encoded.
/// </param>
/// <param name="secureArrayCall">
/// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
/// </param>
/// <returns>
/// True on success; false otherwise.
/// </returns>
public static bool Verify(
string encoded,
string password,
SecureArrayCall secureArrayCall = null)
{
return Verify(encoded, password, null, secureArrayCall);
}
}
}