@@ -51,26 +51,16 @@ public class Argon2Function extends AbstractHashingFunction
51
51
private static final int ARGON2_BLOCK_SIZE = 1024 ;
52
52
53
53
public static final int ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8 ;
54
-
55
- private ExecutorService service ;
56
-
57
54
private final int iterations ;
58
-
59
55
private final int memory ;
60
-
61
56
private final long [][] initialBlockMemory ;
62
-
63
57
private final int parallelism ;
64
-
65
58
private final int outputLength ;
66
-
67
59
private final int segmentLength ;
68
-
69
60
private final Argon2 variant ;
70
-
71
61
private final int version ;
72
-
73
62
private final int laneLength ;
63
+ private ExecutorService service ;
74
64
75
65
Argon2Function (int memory , int iterations , int parallelism , int outputLength , Argon2 variant , int version )
76
66
{
@@ -136,7 +126,7 @@ public static Argon2Function getInstance(int memory, int iterations, int paralle
136
126
* @since 1.5.0
137
127
*/
138
128
public static Argon2Function getInstance (int memory , int iterations , int parallelism , int outputLength , Argon2 type ,
139
- int version )
129
+ int version )
140
130
{
141
131
String key = getUID (memory , iterations , parallelism , outputLength , type , version );
142
132
if (INSTANCES .containsKey (key ))
@@ -171,99 +161,6 @@ public static Argon2Function getInstanceFromHash(String hashed)
171
161
return getInstance (memory , iterations , parallelism , outputLength , type , version );
172
162
}
173
163
174
- @ Override
175
- public Hash hash (CharSequence plainTextPassword )
176
- {
177
- byte [] salt = SaltGenerator .generate ();
178
- return internalHash (Utils .fromCharSequenceToBytes (plainTextPassword ), salt , null );
179
- }
180
-
181
- @ Override
182
- public Hash hash (byte [] plainTextPassword )
183
- {
184
- byte [] salt = SaltGenerator .generate ();
185
- return internalHash (plainTextPassword , salt , null );
186
- }
187
-
188
- @ Override
189
- public Hash hash (CharSequence plainTextPassword , String salt )
190
- {
191
- return hash (plainTextPassword , salt , null );
192
- }
193
-
194
- @ Override
195
- public Hash hash (byte [] plainTextPassword , byte [] salt )
196
- {
197
- return hash (plainTextPassword , salt , null );
198
- }
199
-
200
- @ Override
201
- public Hash hash (CharSequence plainTextPassword , String salt , CharSequence pepper )
202
- {
203
- return internalHash (Utils .fromCharSequenceToBytes (plainTextPassword ), Utils .fromCharSequenceToBytes (salt ), pepper );
204
- }
205
-
206
- @ Override
207
- public Hash hash (byte [] plainTextPassword , byte [] salt , CharSequence pepper )
208
- {
209
- return internalHash (plainTextPassword , salt , pepper );
210
- }
211
-
212
- private Hash internalHash (byte [] plainTextPassword , byte [] salt , CharSequence pepper )
213
- {
214
- long [][] blockMemory = copyOf (initialBlockMemory );
215
-
216
- if (salt == null )
217
- {
218
- salt = SaltGenerator .generate ();
219
- }
220
- initialize (plainTextPassword , salt , Utils .fromCharSequenceToBytes (pepper ), null , blockMemory );
221
- fillMemoryBlocks (blockMemory );
222
- byte [] hash = ending (blockMemory );
223
- Hash result = new Hash (this , encodeHash (hash , salt ), hash , salt );
224
- result .setPepper (pepper );
225
- return result ;
226
- }
227
-
228
- @ Override
229
- public boolean check (CharSequence plainTextPassword , String hashed )
230
- {
231
- return check (plainTextPassword , hashed , null , null );
232
- }
233
-
234
- @ Override
235
- public boolean check (byte [] plainTextPassword , byte [] hashed )
236
- {
237
- return check (plainTextPassword , hashed , null , null );
238
- }
239
-
240
- @ Override
241
- public boolean check (CharSequence plainTextPassword , String hashed , String salt , CharSequence pepper )
242
- {
243
- byte [] plainTextPasswordAsBytes = Utils .fromCharSequenceToBytes (plainTextPassword );
244
- byte [] saltAsBytes = Utils .fromCharSequenceToBytes (salt );
245
- byte [] hashedAsBytes = Utils .fromCharSequenceToBytes (hashed );
246
- return check (plainTextPasswordAsBytes , hashedAsBytes , saltAsBytes , pepper );
247
- }
248
-
249
- @ Override
250
- public boolean check (byte [] plainTextPassword , byte [] hashed , byte [] salt , CharSequence pepper )
251
- {
252
- byte [] theSalt ;
253
- if (salt == null || salt .length == 0 )
254
- {
255
- Object [] params = decodeHash (Utils .fromBytesToString (hashed ));
256
- theSalt = (byte []) params [5 ];
257
- }
258
- else
259
- {
260
- theSalt = salt ;
261
- }
262
-
263
- Hash internalHash = internalHash (plainTextPassword , theSalt , pepper );
264
- return slowEquals (internalHash .getResultAsBytes (), hashed );
265
- }
266
-
267
164
protected static String getUID (int memory , int iterations , int parallelism , int outputLength , Argon2 type , int version )
268
165
{
269
166
return memory + "|" + iterations + "|" + parallelism + "|" + outputLength + "|" + type .ordinal () + "|" + version ;
@@ -346,8 +243,8 @@ private static void fillBlock(long[] x, long[] y, long[] currentBlock, boolean w
346
243
}
347
244
348
245
private static void roundFunction (long [] block , int v0 , int v1 , int v2 , int v3 , int v4 , int v5 , int v6 , int v7 , int v8 ,
349
- int v9 , // NOSONAR
350
- int v10 , int v11 , int v12 , int v13 , int v14 , int v15 )
246
+ int v9 , // NOSONAR
247
+ int v10 , int v11 , int v12 , int v13 , int v14 , int v15 )
351
248
{
352
249
f (block , v0 , v4 , v8 , v12 );
353
250
f (block , v1 , v5 , v9 , v13 );
@@ -418,7 +315,103 @@ protected static String toString(int memory, int iterations, int parallelism, in
418
315
.name () + ", v=" + version ;
419
316
}
420
317
318
+ private static String remove (String source , String remove )
319
+ {
320
+ return source .substring (remove .length ());
321
+ }
322
+
323
+ @ Override
324
+ public Hash hash (CharSequence plainTextPassword )
325
+ {
326
+ byte [] salt = SaltGenerator .generate ();
327
+ return internalHash (Utils .fromCharSequenceToBytes (plainTextPassword ), salt , null );
328
+ }
329
+
330
+ @ Override
331
+ public Hash hash (byte [] plainTextPassword )
332
+ {
333
+ byte [] salt = SaltGenerator .generate ();
334
+ return internalHash (plainTextPassword , salt , null );
335
+ }
336
+
337
+ @ Override
338
+ public Hash hash (CharSequence plainTextPassword , String salt )
339
+ {
340
+ return hash (plainTextPassword , salt , null );
341
+ }
342
+
343
+ @ Override
344
+ public Hash hash (byte [] plainTextPassword , byte [] salt )
345
+ {
346
+ return hash (plainTextPassword , salt , null );
347
+ }
348
+
349
+ @ Override
350
+ public Hash hash (CharSequence plainTextPassword , String salt , CharSequence pepper )
351
+ {
352
+ return internalHash (Utils .fromCharSequenceToBytes (plainTextPassword ), Utils .fromCharSequenceToBytes (salt ), pepper );
353
+ }
354
+
355
+ @ Override
356
+ public Hash hash (byte [] plainTextPassword , byte [] salt , CharSequence pepper )
357
+ {
358
+ return internalHash (plainTextPassword , salt , pepper );
359
+ }
421
360
361
+ private Hash internalHash (byte [] plainTextPassword , byte [] salt , CharSequence pepper )
362
+ {
363
+ long [][] blockMemory = copyOf (initialBlockMemory );
364
+
365
+ if (salt == null )
366
+ {
367
+ salt = SaltGenerator .generate ();
368
+ }
369
+ initialize (plainTextPassword , salt , Utils .fromCharSequenceToBytes (pepper ), null , blockMemory );
370
+ fillMemoryBlocks (blockMemory );
371
+ byte [] hash = ending (blockMemory );
372
+ Hash result = new Hash (this , encodeHash (hash , salt ), hash , salt );
373
+ result .setPepper (pepper );
374
+ return result ;
375
+ }
376
+
377
+ @ Override
378
+ public boolean check (CharSequence plainTextPassword , String hashed )
379
+ {
380
+ return check (plainTextPassword , hashed , null , null );
381
+ }
382
+
383
+ @ Override
384
+ public boolean check (byte [] plainTextPassword , byte [] hashed )
385
+ {
386
+ return check (plainTextPassword , hashed , null , null );
387
+ }
388
+
389
+ @ Override
390
+ public boolean check (CharSequence plainTextPassword , String hashed , String salt , CharSequence pepper )
391
+ {
392
+ byte [] plainTextPasswordAsBytes = Utils .fromCharSequenceToBytes (plainTextPassword );
393
+ byte [] saltAsBytes = Utils .fromCharSequenceToBytes (salt );
394
+ byte [] hashedAsBytes = Utils .fromCharSequenceToBytes (hashed );
395
+ return check (plainTextPasswordAsBytes , hashedAsBytes , saltAsBytes , pepper );
396
+ }
397
+
398
+ @ Override
399
+ public boolean check (byte [] plainTextPassword , byte [] hashed , byte [] salt , CharSequence pepper )
400
+ {
401
+ byte [] theSalt ;
402
+ if (salt == null || salt .length == 0 )
403
+ {
404
+ Object [] params = decodeHash (Utils .fromBytesToString (hashed ));
405
+ theSalt = (byte []) params [5 ];
406
+ }
407
+ else
408
+ {
409
+ theSalt = salt ;
410
+ }
411
+
412
+ Hash internalHash = internalHash (plainTextPassword , theSalt , pepper );
413
+ return slowEquals (internalHash .getResultAsBytes (), hashed );
414
+ }
422
415
423
416
/**
424
417
* @return the memory in bytes
@@ -496,8 +489,8 @@ private void initialize(byte[] plainTextPassword, byte[] salt, byte[] secret, by
496
489
byte [] initialHash = new byte [64 ];
497
490
blake2b .doFinal (initialHash , 0 );
498
491
499
- final byte [] zeroBytes = { 0 , 0 , 0 , 0 };
500
- final byte [] oneBytes = { 1 , 0 , 0 , 0 };
492
+ final byte [] zeroBytes = {0 , 0 , 0 , 0 };
493
+ final byte [] oneBytes = {1 , 0 , 0 , 0 };
501
494
502
495
byte [] initialHashWithZeros = getInitialHashLong (initialHash , zeroBytes );
503
496
byte [] initialHashWithOnes = getInitialHashLong (initialHash , oneBytes );
@@ -696,7 +689,7 @@ private int rotatePrevOffset(int currentOffset, int prevOffset)
696
689
}
697
690
698
691
private long getPseudoRandom (int index , long [] addressBlock , long [] inputBlock , long [] zeroBlock , int prevOffset ,
699
- boolean dataIndependentAddressing , long [][] blockMemory )
692
+ boolean dataIndependentAddressing , long [][] blockMemory )
700
693
{
701
694
if (dataIndependentAddressing )
702
695
{
@@ -724,7 +717,7 @@ private int getRefLane(int pass, int lane, int slice, long pseudoRandom)
724
717
}
725
718
726
719
private void initAddressBlocks (int pass , int lane , int slice , long [] zeroBlock , long [] inputBlock , long [] addressBlock ,
727
- long [][] blockMemory )
720
+ long [][] blockMemory )
728
721
{
729
722
inputBlock [0 ] = Utils .intToLong (pass );
730
723
inputBlock [1 ] = Utils .intToLong (lane );
@@ -831,11 +824,6 @@ private long[][] copyOf(long[][] old)
831
824
return current ;
832
825
}
833
826
834
- private static String remove (String source , String remove )
835
- {
836
- return source .substring (remove .length ());
837
- }
838
-
839
827
private String encodeHash (byte [] hash , byte [] salt )
840
828
{
841
829
return "$argon2" + variant .name ()
0 commit comments