Skip to content

Commit 82c16d0

Browse files
committed
Fixes ugly Argon initialization by delegating ArgonConfig method instead
1 parent 473c3a5 commit 82c16d0

3 files changed

Lines changed: 23 additions & 30 deletions

File tree

README.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,14 @@ An [argon2](https://github.com/P-H-C/phc-winner-argon2) binding using Java Forei
99

1010
```java
1111

12-
import com.zylquinal.argon2.*;
12+
import com.zylquinal.argon2.api.*;
1313

1414
public class Example {
1515

1616
public static void main(String[] args) {
17-
Argon2 argon2 = Argon2.create(2, 65536, 1);
18-
19-
byte[] rawHash = argon2.hashRaw("password".getBytes(), "randomsalt".getBytes());
20-
String encodedHash = argon2.hashEncoded("password".getBytes(), "randomsalt".getBytes());
21-
22-
rawHash = ArgonContext.builder()
23-
.password("password".getBytes())
24-
.salt("randomsalt".getBytes())
25-
.memoryCost(65536)
26-
.iterations(2)
27-
.parallelism(1)
28-
.hashLength(32)
29-
.flag(ArgonFlag.CLEAR_ALL)
30-
.version(ArgonVersion.VERSION_13)
31-
.hash(ArgonVariant.ARGON_2ID);
17+
ArgonConfig config = ArgonConfig.of(65536, 1, 2);
18+
Argon argon = Argon.of(config);
19+
byte[] rawHash = argon.rawHash("password".getBytes(), "randomsalt".getBytes());
3220
}
3321

3422
}

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group 'com.zylquinal.argon2'
6-
version '2.0-SNAPSHOT'
6+
version '2.1-SNAPSHOT'
77

88
repositories {
99
mavenCentral()
@@ -16,7 +16,6 @@ dependencies {
1616
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
1717
testImplementation 'de.mkammerer:argon2-jvm:2.11'
1818
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
19-
2019
}
2120

2221
tasks.withType(JavaCompile).configureEach {

src/main/java/com/zylquinal/argon2/api/Argon.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.zylquinal.argon2.api;
22

3-
import com.zylquinal.argon2.api.enumeration.ArgonFlag;
4-
import com.zylquinal.argon2.api.enumeration.ArgonVariant;
5-
import com.zylquinal.argon2.api.enumeration.ArgonVersion;
63
import com.zylquinal.argon2.api.exception.ArgonException;
4+
import lombok.experimental.Delegate;
75
import org.jetbrains.annotations.NotNull;
86
import org.jetbrains.annotations.Nullable;
97

10-
public record Argon(ArgonConfig config, int iterations, int memoryCost, int parallelism, int hashLength, ArgonVersion version,
11-
ArgonFlag flat, ArgonVariant variant) {
8+
public class Argon {
9+
10+
@Delegate
11+
private final ArgonConfig config;
12+
13+
private Argon(ArgonConfig config) {
14+
this.config = config;
15+
}
1216

1317
/**
1418
* Hashes the given password using the given salt.
@@ -46,8 +50,9 @@ public ComputedHash hash(byte @NotNull [] password, byte @NotNull [] salt, byte
4650
* @throws ArgonException if the hash could not be computed
4751
*/
4852
public ComputedHash hash(byte @NotNull [] password, byte @NotNull [] salt, byte @Nullable [] secret, byte @Nullable [] associatedData) {
49-
byte[] rawHash = ArgonDirect.rawHash(password, salt, secret, associatedData, hashLength, memoryCost, parallelism, iterations, version, flat, variant);
50-
return ComputedHash.of(rawHash, salt, config());
53+
byte[] rawHash = ArgonDirect.rawHash(password, salt, secret, associatedData, hashLength(), memoryCost(), parallelism(),
54+
iterations(), version(), flag(), variant());
55+
return ComputedHash.of(rawHash, salt, config);
5156
}
5257

5358
/**
@@ -86,7 +91,8 @@ public byte[] rawHash(byte[] password, byte[] salt, byte @Nullable [] secret) {
8691
* @throws ArgonException if the hash could not be computed
8792
*/
8893
public byte[] rawHash(byte @NotNull [] password, byte @NotNull [] salt, byte @Nullable [] secret, byte @Nullable [] associatedData) {
89-
return ArgonDirect.rawHash(password, salt, secret, associatedData, hashLength, memoryCost, parallelism, iterations, version, flat, variant);
94+
return ArgonDirect.rawHash(password, salt, secret, associatedData, hashLength(), memoryCost(), parallelism(),
95+
iterations(), version(), flag(), variant());
9096
}
9197

9298
/**
@@ -125,7 +131,7 @@ public boolean verify(@NotNull String encodedHash, byte @NotNull [] password, b
125131
* @throws ArgonException if the hash could not be computed
126132
*/
127133
public boolean verify(@NotNull String encodedHash, byte @NotNull [] password, byte @Nullable [] secret, byte @Nullable [] associatedData) {
128-
return ArgonDirect.verify(encodedHash, password, secret, associatedData, flat);
134+
return ArgonDirect.verify(encodedHash, password, secret, associatedData, flag());
129135
}
130136

131137
/**
@@ -188,7 +194,8 @@ public boolean verify(@NotNull ComputedHash hash, byte @NotNull [] password, byt
188194
* @throws ArgonException if the hash could not be computed
189195
*/
190196
public boolean verify(byte @NotNull [] hash, byte @NotNull [] password, byte @NotNull [] salt, byte @Nullable [] secret, byte @Nullable [] associatedData) {
191-
return ArgonDirect.verify(hash, password, salt, secret, associatedData, hashLength, memoryCost, parallelism, iterations, version, flat, variant);
197+
return ArgonDirect.verify(hash, password, salt, secret, associatedData, hashLength(), memoryCost(), parallelism(),
198+
iterations(), version(), flag(), variant());
192199
}
193200

194201
/**
@@ -198,8 +205,7 @@ public boolean verify(byte @NotNull [] hash, byte @NotNull [] password, byte @No
198205
* @return a new {@link Argon} instance
199206
*/
200207
public static Argon of(ArgonConfig config) {
201-
return new Argon(config, config.iterations(), config.memoryCost(), config.parallelism(), config.hashLength(),
202-
config.version(), config.flag(), config.variant());
208+
return new Argon(config);
203209
}
204210

205211
}

0 commit comments

Comments
 (0)