Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit cb2ce18

Browse files
authored
Merge pull request #10 from genexuslabs/issue#82272
Issue#82272
2 parents 12a82f5 + 09e7c74 commit cb2ce18

File tree

11 files changed

+174
-9
lines changed

11 files changed

+174
-9
lines changed

GeneXusCryptography/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.genexus</groupId>
99
<artifactId>SecurityAPIParent</artifactId>
10-
<version>1.0.0.2</version>
10+
<version>1.0.0.3</version>
1111
</parent>
1212

1313

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.genexus.cryptography.passwordDerivation;
2+
3+
import org.bouncycastle.crypto.params.Argon2Parameters;
4+
5+
import com.genexus.securityapicommons.commons.Error;
6+
7+
public enum Argon2HashType {
8+
ARGON2_d, ARGON2_i, ARGON2_id;
9+
10+
public static Argon2HashType getArgon2HashType(String argon2HashType, Error error) {
11+
switch (argon2HashType) {
12+
case "ARGON2_d":
13+
return Argon2HashType.ARGON2_d;
14+
case "ARGON2_i":
15+
return Argon2HashType.ARGON2_i;
16+
case "ARGON2_id":
17+
return Argon2HashType.ARGON2_id;
18+
default:
19+
error.setError("A2001", "Unrecognized Arggon2HashType");
20+
return null;
21+
}
22+
23+
}
24+
25+
public static String valueOf(Argon2HashType argon2HashType, Error error) {
26+
switch (argon2HashType) {
27+
case ARGON2_d:
28+
return "ARGON2_d";
29+
case ARGON2_i:
30+
return "ARGON2_i";
31+
case ARGON2_id:
32+
return "ARGON2_id";
33+
default:
34+
error.setError("A2002", "Unrecognized Arggon2HashType");
35+
return "";
36+
}
37+
}
38+
39+
public static int getArgon2Parameter(Argon2HashType argon2HashType, Error error) {
40+
switch (argon2HashType) {
41+
case ARGON2_d:
42+
return Argon2Parameters.ARGON2_d;
43+
case ARGON2_i:
44+
return Argon2Parameters.ARGON2_i;
45+
case ARGON2_id:
46+
return Argon2Parameters.ARGON2_id;
47+
default:
48+
error.setError("A2003", "Unrecognized Arggon2HashType");
49+
return 0;
50+
}
51+
}
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.genexus.cryptography.passwordDerivation;
2+
3+
import org.bouncycastle.crypto.params.Argon2Parameters;
4+
5+
import com.genexus.securityapicommons.commons.Error;
6+
7+
public enum Argon2Version {
8+
9+
ARGON2_VERSION_10, ARGON2_VERSION_13;
10+
11+
public static Argon2Version getArgon2Version(String argon2Version, Error error) {
12+
switch (argon2Version) {
13+
case "ARGON2_VERSION_10":
14+
return Argon2Version.ARGON2_VERSION_10;
15+
case "ARGON2_VERSION_13":
16+
return Argon2Version.ARGON2_VERSION_13;
17+
default:
18+
error.setError("AR001", "Unrecognized Argon2Version");
19+
return null;
20+
}
21+
}
22+
23+
public static String valueOf(Argon2Version argon2Version, Error error) {
24+
switch (argon2Version) {
25+
case ARGON2_VERSION_10:
26+
return "ARGON2_VERSION_10";
27+
case ARGON2_VERSION_13:
28+
return "ARGON2_VERSION_13";
29+
default:
30+
error.setError("AR002", "Unrecognized Argon2Version");
31+
return "";
32+
}
33+
}
34+
35+
public static int getVersionParameter(Argon2Version argon2Version, Error error) {
36+
switch (argon2Version) {
37+
case ARGON2_VERSION_10:
38+
return Argon2Parameters.ARGON2_VERSION_10;
39+
case ARGON2_VERSION_13:
40+
return Argon2Parameters.ARGON2_VERSION_13;
41+
default:
42+
error.setError("AR003", "Unrecognized Argon2Version");
43+
return 0;
44+
}
45+
}
46+
47+
}

GeneXusCryptography/src/main/java/com/genexus/cryptography/passwordDerivation/PasswordDerivation.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.genexus.cryptography.passwordDerivation;
22

3+
import org.bouncycastle.crypto.generators.Argon2BytesGenerator;
34
import org.bouncycastle.crypto.generators.BCrypt;
45
import org.bouncycastle.crypto.generators.SCrypt;
6+
import org.bouncycastle.crypto.params.Argon2Parameters;
57
import org.bouncycastle.util.Strings;
68
import org.bouncycastle.util.encoders.Base64;
9+
import org.bouncycastle.util.encoders.Hex;
710

811
import com.genexus.cryptography.commons.PasswordDerivationObject;
912
import com.genexus.securityapicommons.config.EncodingUtil;
@@ -101,7 +104,7 @@ public String doGenerateBcrypt(String password, String salt, int cost) {
101104
byte[] encryptedBytes = BCrypt.generate(eu.getBytes(password), Strings.toByteArray(hexa.fromHexa(salt)), cost);
102105
String result = Strings.fromByteArray(Base64.encode(encryptedBytes));
103106
if (result == null || result.length() == 0) {
104-
this.error.setError("PD010", "Brypt generation error");
107+
this.error.setError("PD010", "Bcrypt generation error");
105108
return "";
106109
}
107110
this.error.cleanError();
@@ -123,6 +126,49 @@ public String doGenerateDefaultBcrypt(String password, String salt) {
123126
return doGenerateBcrypt(password, salt, cost);
124127
}
125128

129+
public String doGenerateArgon2(String argon2Version10, String argon2HashType, int iterations, int memory,
130+
int parallelism, String password, String salt, int hashLength) {
131+
if (!areArgon2ValidParameters(iterations, parallelism, hashLength)) {
132+
return "";
133+
}
134+
Argon2Version ver_aux = Argon2Version.getArgon2Version(argon2Version10, this.error);
135+
int version = Argon2Version.getVersionParameter(ver_aux, this.error);
136+
if (this.hasError()) {
137+
return "";
138+
}
139+
Argon2HashType hash_aux = Argon2HashType.getArgon2HashType(argon2HashType, this.error);
140+
int hashType = Argon2HashType.getArgon2Parameter(hash_aux, this.error);
141+
if (this.hasError()) {
142+
return "";
143+
}
144+
145+
EncodingUtil eu = new EncodingUtil();
146+
HexaEncoder hexa = new HexaEncoder();
147+
byte[] bytePass = eu.getBytes(password);
148+
if(eu.hasError())
149+
{
150+
this.error = eu.getError();
151+
return "";
152+
}
153+
154+
Argon2Parameters.Builder builder = new Argon2Parameters.Builder(hashType).withVersion(version)
155+
.withIterations(iterations).withMemoryPowOfTwo(memory).withParallelism(parallelism)
156+
.withSalt(Strings.toByteArray(hexa.fromHexa(salt)));
157+
158+
Argon2BytesGenerator dig = new Argon2BytesGenerator();
159+
dig.init(builder.build());
160+
byte[] res = new byte[hashLength];
161+
dig.generateBytes(bytePass, res);
162+
String result = Strings.fromByteArray(Base64.encode(res));
163+
if (result == null || result.length() == 0) {
164+
this.error.setError("PD012", "Argon2 generation error");
165+
return "";
166+
}
167+
this.error.cleanError();
168+
return result;
169+
170+
}
171+
126172
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
127173

128174
/**
@@ -194,4 +240,20 @@ private boolean areSCRyptValidParameters(int CPUCost, int blockSize, int paralle
194240
}
195241
return true;
196242
}
243+
244+
private boolean areArgon2ValidParameters(int iterations, int parallelism, int hashLength) {
245+
if (parallelism < 1 || parallelism >= 16777216) {
246+
this.error.setError("PD012", "Parallelism parameter must be >= 1 and < 16777216");
247+
return false;
248+
}
249+
if (iterations < 1) {
250+
this.error.setError("PD013", "Must be 1 iteration at least");
251+
return false;
252+
}
253+
if (hashLength < 4) {
254+
this.error.setError("PD014", "The output hash length must be >= 4");
255+
return false;
256+
}
257+
return true;
258+
}
197259
}

GeneXusCryptography/src/main/java/com/genexus/cryptography/passwordDerivation/PasswordDerivationAlgorithm.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*/
99
public enum PasswordDerivationAlgorithm {
10-
SCrypt, Bcrypt,;
10+
SCrypt, Bcrypt,Argon2;
1111

1212
/**
1313
* Mapping between String name and PasswordDerivationAlgorithm enum
@@ -26,6 +26,8 @@ public static PasswordDerivationAlgorithm getPasswordDerivationAlgorithm(String
2626
return PasswordDerivationAlgorithm.SCrypt;
2727
case "Bcrypt":
2828
return PasswordDerivationAlgorithm.Bcrypt;
29+
case "Argon2":
30+
return PasswordDerivationAlgorithm.Argon2;
2931
default:
3032
error.setError("PD001", "Unrecognized PasswordDerivationAlgorithm");
3133
return null;
@@ -45,6 +47,8 @@ public static String valueOf(PasswordDerivationAlgorithm passwordDerivationAlgor
4547
return "SCrypt";
4648
case Bcrypt:
4749
return "Bcrypt";
50+
case Argon2:
51+
return "Argon2";
4852
default:
4953
error.setError("PD002", "Unrecognized PasswordDerivationAlgorithm");
5054
return "Unrecognized algorithm";

GeneXusFtps/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.genexus</groupId>
1010
<artifactId>SecurityAPIParent</artifactId>
11-
<version>1.0.0.2</version>
11+
<version>1.0.0.3</version>
1212
</parent>
1313

1414

GeneXusJWT/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.genexus</groupId>
99
<artifactId>SecurityAPIParent</artifactId>
10-
<version>1.0.0.2</version>
10+
<version>1.0.0.3</version>
1111
</parent>
1212

1313

GeneXusSftp/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.genexus</groupId>
99
<artifactId>SecurityAPIParent</artifactId>
10-
<version>1.0.0.2</version>
10+
<version>1.0.0.3</version>
1111
</parent>
1212

1313

GeneXusXmlSignature/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.genexus</groupId>
99
<artifactId>SecurityAPIParent</artifactId>
10-
<version>1.0.0.2</version>
10+
<version>1.0.0.3</version>
1111
</parent>
1212

1313
<artifactId>GeneXusXmlSignature</artifactId>

SecurityAPICommons/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.genexus</groupId>
99
<artifactId>SecurityAPIParent</artifactId>
10-
<version>1.0.0.2</version>
10+
<version>1.0.0.3</version>
1111
</parent>
1212

1313

0 commit comments

Comments
 (0)