Skip to content

Commit f80271d

Browse files
committed
Merge branch 'master' of github.com:Password4j/password4j
2 parents 7041635 + 69ff99e commit f80271d

17 files changed

+496
-489
lines changed

src/main/java/com/password4j/AlgorithmFinder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public class AlgorithmFinder
5858
*/
5959
private static SecureRandom secureRandom;
6060

61+
static
62+
{
63+
initialize();
64+
}
65+
6166
private AlgorithmFinder()
6267
{
6368
//
@@ -419,9 +424,4 @@ private static class Param
419424
this.length = length;
420425
}
421426
}
422-
423-
static
424-
{
425-
initialize();
426-
}
427427
}

src/main/java/com/password4j/Argon2Function.java

+104-116
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,16 @@ public class Argon2Function extends AbstractHashingFunction
5151
private static final int ARGON2_BLOCK_SIZE = 1024;
5252

5353
public static final int ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8;
54-
55-
private ExecutorService service;
56-
5754
private final int iterations;
58-
5955
private final int memory;
60-
6156
private final long[][] initialBlockMemory;
62-
6357
private final int parallelism;
64-
6558
private final int outputLength;
66-
6759
private final int segmentLength;
68-
6960
private final Argon2 variant;
70-
7161
private final int version;
72-
7362
private final int laneLength;
63+
private ExecutorService service;
7464

7565
Argon2Function(int memory, int iterations, int parallelism, int outputLength, Argon2 variant, int version)
7666
{
@@ -136,7 +126,7 @@ public static Argon2Function getInstance(int memory, int iterations, int paralle
136126
* @since 1.5.0
137127
*/
138128
public static Argon2Function getInstance(int memory, int iterations, int parallelism, int outputLength, Argon2 type,
139-
int version)
129+
int version)
140130
{
141131
String key = getUID(memory, iterations, parallelism, outputLength, type, version);
142132
if (INSTANCES.containsKey(key))
@@ -171,99 +161,6 @@ public static Argon2Function getInstanceFromHash(String hashed)
171161
return getInstance(memory, iterations, parallelism, outputLength, type, version);
172162
}
173163

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-
267164
protected static String getUID(int memory, int iterations, int parallelism, int outputLength, Argon2 type, int version)
268165
{
269166
return memory + "|" + iterations + "|" + parallelism + "|" + outputLength + "|" + type.ordinal() + "|" + version;
@@ -346,8 +243,8 @@ private static void fillBlock(long[] x, long[] y, long[] currentBlock, boolean w
346243
}
347244

348245
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)
351248
{
352249
f(block, v0, v4, v8, v12);
353250
f(block, v1, v5, v9, v13);
@@ -418,7 +315,103 @@ protected static String toString(int memory, int iterations, int parallelism, in
418315
.name() + ", v=" + version;
419316
}
420317

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+
}
421360

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+
}
422415

423416
/**
424417
* @return the memory in bytes
@@ -496,8 +489,8 @@ private void initialize(byte[] plainTextPassword, byte[] salt, byte[] secret, by
496489
byte[] initialHash = new byte[64];
497490
blake2b.doFinal(initialHash, 0);
498491

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};
501494

502495
byte[] initialHashWithZeros = getInitialHashLong(initialHash, zeroBytes);
503496
byte[] initialHashWithOnes = getInitialHashLong(initialHash, oneBytes);
@@ -696,7 +689,7 @@ private int rotatePrevOffset(int currentOffset, int prevOffset)
696689
}
697690

698691
private long getPseudoRandom(int index, long[] addressBlock, long[] inputBlock, long[] zeroBlock, int prevOffset,
699-
boolean dataIndependentAddressing, long[][] blockMemory)
692+
boolean dataIndependentAddressing, long[][] blockMemory)
700693
{
701694
if (dataIndependentAddressing)
702695
{
@@ -724,7 +717,7 @@ private int getRefLane(int pass, int lane, int slice, long pseudoRandom)
724717
}
725718

726719
private void initAddressBlocks(int pass, int lane, int slice, long[] zeroBlock, long[] inputBlock, long[] addressBlock,
727-
long[][] blockMemory)
720+
long[][] blockMemory)
728721
{
729722
inputBlock[0] = Utils.intToLong(pass);
730723
inputBlock[1] = Utils.intToLong(lane);
@@ -831,11 +824,6 @@ private long[][] copyOf(long[][] old)
831824
return current;
832825
}
833826

834-
private static String remove(String source, String remove)
835-
{
836-
return source.substring(remove.length());
837-
}
838-
839827
private String encodeHash(byte[] hash, byte[] salt)
840828
{
841829
return "$argon2" + variant.name()

0 commit comments

Comments
 (0)