Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:gamutils_eo' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrampone authored and Beta Bot committed Jan 15, 2025
1 parent e48d411 commit 7236f9c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 53 deletions.
4 changes: 0 additions & 4 deletions gamutils/src/main/java/com/genexus/gam/GamUtilsEO.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public static String randomHexaBits(int bits) {
return Random.hexaBits(bits);
}

public static String randomUtf8Bits(int bits) {
return Random.utf8Bits(bits);
}

//**JWK**//

public static String generateKeyPair() {
Expand Down
21 changes: 0 additions & 21 deletions gamutils/src/main/java/com/genexus/gam/utils/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,4 @@ public static String hexaBits(int bits)
}
return sb.toString().replaceAll("\\s", "");
}

public static String utf8Bits(int bits)
{
int targetBytes = (bits + 7) / 8;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < targetBytes; i++) {
sb.append("a");
}
String result = sb.toString();
byte[] utf8Bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
if (utf8Bytes.length > targetBytes) {
return new String(utf8Bytes, 0, targetBytes, StandardCharsets.UTF_8);
} else if (utf8Bytes.length < targetBytes) {
StringBuilder paddedString = new StringBuilder(sb.toString());
for (int i = utf8Bytes.length; i < targetBytes; i++) {
paddedString.append("0");
}
return paddedString.toString();
}
return result;
}
}
12 changes: 6 additions & 6 deletions gamutils/src/test/java/com/genexus/gam/utils/test/JwtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public void test_json_Sha256()
" \"alg\": \"HS256\",\n" +
" \"typ\": \"JWT\"\n" +
"}";
int[] lengths = new int[]{256, 512, 1024};
int[] lengths = new int[]{32, 64, 128};
for (int n : lengths) {
String secret = GamUtilsEO.randomUtf8Bits(n);
String secret = GamUtilsEO.randomAlphanumeric(n);
String token = GamUtilsEO.createJwtSha(secret, payload, header);
Assert.assertFalse("test_json_Sha256 create", token.isEmpty());
boolean result = GamUtilsEO.verifyJwtSha(secret, token);
Expand All @@ -166,9 +166,9 @@ public void test_json_Sha512()
" \"alg\": \"HS512\",\n" +
" \"typ\": \"JWT\"\n" +
"}";
int[] lengths = new int[]{512, 1024};
int[] lengths = new int[]{64, 128};
for (int n : lengths) {
String secret = GamUtilsEO.randomUtf8Bits(n);
String secret = GamUtilsEO.randomAlphanumeric(n);
String token = GamUtilsEO.createJwtSha(secret, payload, header);
Assert.assertFalse("test_json_Sha512 create", token.isEmpty());
boolean result = GamUtilsEO.verifyJwtSha(secret, token);
Expand All @@ -183,7 +183,7 @@ public void test_VerifyAlgorithm_True()
" \"alg\": \"HS512\",\n" +
" \"typ\": \"JWT\"\n" +
"}";
String secret = GamUtilsEO.randomUtf8Bits(512);
String secret = GamUtilsEO.randomAlphanumeric(64);
String token = GamUtilsEO.createJwtSha(secret, payload, header);
boolean resultSha512 = GamUtilsEO.verifyAlgorithm("HS512", token);
Assert.assertTrue("test_VerifyAlgorithm_True", resultSha512);
Expand All @@ -196,7 +196,7 @@ public void test_VerifyAlgorithm_False()
" \"alg\": \"HS512\",\n" +
" \"typ\": \"JWT\"\n" +
"}";
String secret = GamUtilsEO.randomUtf8Bits(512);
String secret = GamUtilsEO.randomAlphanumeric(64);
String token = GamUtilsEO.createJwtSha(secret, payload, header);
boolean resultSha512 = GamUtilsEO.verifyAlgorithm("RS256", token);
Assert.assertFalse("test_VerifyAlgorithm_False", resultSha512);
Expand Down
22 changes: 0 additions & 22 deletions gamutils/src/test/java/com/genexus/gam/utils/test/RandomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,4 @@ public void testHexaBits() {
}
}
}

@Test
public void TestUtf8Bits()
{
int[] lengths = new int[]{32, 64, 128, 256, 512, 1024};

for (int n : lengths) {
String utf8 = GamUtilsEO.randomUtf8Bits(n);
Assert.assertFalse("TestUtf8Bits", utf8.isEmpty());
try
{
byte[] decoded = utf8.getBytes(StandardCharsets.UTF_8);//Hex.decode(hexa);
if(decoded.length != (n + 7) / 8)
{
Assert.fail("TestUtf8Bits wrong utf8 length");
}
}catch(Exception e)
{
Assert.fail("TestUtf8Bits not utf8 characters" + e.getMessage());
}
}
}
}

0 comments on commit 7236f9c

Please sign in to comment.