Repository: wultra/powerauth-crypto (java)

Issue:
Update to JDK 17

Pull request:
Fix #469: Update to JDK 17

Patch:
diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml
index e9f3eb91..48843127 100644
--- a/.github/workflows/codeql-analysis.yml
+++ b/.github/workflows/codeql-analysis.yml
@@ -30,12 +30,19 @@ jobs:
 
     steps:
     - name: Checkout repository
-      uses: actions/checkout@v2
+      uses: actions/checkout@v3
       with:
         # We must fetch at least the immediate parents so that if this is
         # a pull request then we can checkout the head.
         fetch-depth: 2
 
+    - name: Set up JDK 17
+      uses: actions/setup-java@v3
+      with:
+        java-version: 17
+        distribution: 'temurin'
+        cache: maven
+
     # Initializes the CodeQL tools for scanning.
     - name: Initialize CodeQL
       uses: github/codeql-action/init@v2
diff --git a/pom.xml b/pom.xml
index a2f2e102..6f8b034e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -71,8 +71,8 @@
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-        <maven.compiler.source>1.8</maven.compiler.source>
-        <maven.compiler.target>1.8</maven.compiler.target>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
         <maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
         <maven-deploy-plugin.version>3.0.0</maven-deploy-plugin.version>
         <maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
diff --git a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/config/SignatureConfiguration.java b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/config/SignatureConfiguration.java
index 80858a3c..66f85505 100644
--- a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/config/SignatureConfiguration.java
+++ b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/config/SignatureConfiguration.java
@@ -47,10 +47,10 @@ public abstract class SignatureConfiguration {
 
     public static SignatureConfiguration forFormat(PowerAuthSignatureFormat format) throws CryptoProviderException {
         switch (format) {
-            case BASE64: {
+            case BASE64 -> {
                 return new Base64SignatureConfiguration();
             }
-            case DECIMAL: {
+            case DECIMAL -> {
                 return new DecimalSignatureConfiguration();
             }
         }
diff --git a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/encryptor/ecies/EciesFactory.java b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/encryptor/ecies/EciesFactory.java
index 7ae6bf53..8d0a178c 100644
--- a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/encryptor/ecies/EciesFactory.java
+++ b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/encryptor/ecies/EciesFactory.java
@@ -91,21 +91,17 @@ public class EciesFactory {
      */
     private EciesEncryptor getEciesEncryptor(EciesScope eciesScope, ECPublicKey publicKey, byte[] applicationSecret, byte[] transportKey, byte[] sharedInfo1) throws GenericCryptoException, CryptoProviderException {
         switch (eciesScope) {
-
-            case APPLICATION_SCOPE: {
+            case APPLICATION_SCOPE -> {
                 // Compute hash from APP_SECRET as sharedInfo2
                 byte[] sharedInfo2 = Hash.sha256(applicationSecret);
                 return new EciesEncryptor(publicKey, sharedInfo1, sharedInfo2);
             }
-
-            case ACTIVATION_SCOPE: {
+            case ACTIVATION_SCOPE -> {
                 // The sharedInfo2 is defined as HMAC_SHA256(key: KEY_TRANSPORT, data: APP_SECRET)
                 byte[] sharedInfo2 = hmacHashUtilities.hash(transportKey, applicationSecret);
                 return new EciesEncryptor(publicKey, sharedInfo1, sharedInfo2);
             }
-
-            default:
-                throw new GenericCryptoException("Unsupported ECIES scope: "+eciesScope);
+            default -> throw new GenericCryptoException("Unsupported ECIES scope: " + eciesScope);
         }
     }
 
@@ -165,21 +161,17 @@ public class EciesFactory {
      */
     private EciesDecryptor getEciesDecryptor(EciesScope eciesScope, ECPrivateKey privateKey, byte[] applicationSecret, byte[] transportKey, byte[] sharedInfo1) throws GenericCryptoException, CryptoProviderException {
         switch (eciesScope) {
-
-            case APPLICATION_SCOPE: {
+            case APPLICATION_SCOPE -> {
                 // Compute hash from APP_SECRET as sharedInfo2
                 byte[] sharedInfo2 = Hash.sha256(applicationSecret);
                 return new EciesDecryptor(privateKey, sharedInfo1, sharedInfo2);
             }
-
-            case ACTIVATION_SCOPE: {
+            case ACTIVATION_SCOPE -> {
                 // The sharedInfo2 is defined as HMAC_SHA256(key: KEY_TRANSPORT, data: APP_SECRET)
                 byte[] sharedInfo2 = hmacHashUtilities.hash(transportKey, applicationSecret);
                 return new EciesDecryptor(privateKey, sharedInfo1, sharedInfo2);
             }
-
-            default:
-                throw new GenericCryptoException("Unsupported ECIES scope: "+eciesScope);
+            default -> throw new GenericCryptoException("Unsupported ECIES scope: " + eciesScope);
         }
     }
 
diff --git a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/ECPublicKeyFingerprint.java b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/ECPublicKeyFingerprint.java
index bcd86646..2c13b6de 100644
--- a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/ECPublicKeyFingerprint.java
+++ b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/ECPublicKeyFingerprint.java
@@ -59,12 +59,10 @@ public class ECPublicKeyFingerprint {
             // Prepare fingerprint data
             byte[] fingerprintData;
             switch (activationVersion) {
-                case VERSION_2:
+                case VERSION_2 ->
                     // In version 2 the activation fingerprint is computed from device public key bytes
-                    fingerprintData = toByteArray(devicePublicKey);
-                    break;
-
-                case VERSION_3:
+                        fingerprintData = toByteArray(devicePublicKey);
+                case VERSION_3 -> {
                     if (serverPublicKey == null) {
                         throw new GenericCryptoException("Server public key is invalid");
                     }
@@ -80,10 +78,8 @@ public class ECPublicKeyFingerprint {
                     dataBuffer.put(activationIdBytes);
                     dataBuffer.put(serverPublicKeyBytes);
                     fingerprintData = dataBuffer.array();
-                    break;
-
-                default:
-                    throw new GenericCryptoException("Unsupported activation version: "+activationVersion);
+                }
+                default -> throw new GenericCryptoException("Unsupported activation version: " + activationVersion);
             }
 
             // Calculate fingerprint
diff --git a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/KeyConvertor.java b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/KeyConvertor.java
index 55708413..c0fb4f9d 100644
--- a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/KeyConvertor.java
+++ b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/KeyConvertor.java
@@ -27,7 +27,6 @@ import org.slf4j.LoggerFactory;
 import javax.crypto.SecretKey;
 import javax.crypto.spec.SecretKeySpec;
 import java.math.BigInteger;
-import java.nio.ByteBuffer;
 import java.security.*;
 import java.security.interfaces.ECPrivateKey;
 import java.security.interfaces.ECPublicKey;
diff --git a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/SignatureUtils.java b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/SignatureUtils.java
index 36409fbd..316b2297 100644
--- a/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/SignatureUtils.java
+++ b/powerauth-java-crypto/src/main/java/io/getlime/security/powerauth/crypto/lib/util/SignatureUtils.java
@@ -260,16 +260,15 @@ public class SignatureUtils {
             throw new GenericCryptoException("Invalid length of signature counter");
         }
         switch (configuration.getSignatureFormat()) {
-            case BASE64: {
+            case BASE64 -> {
                 return computePowerAuthBase64Signature(data, signatureKeys, ctrData);
             }
-            case DECIMAL: {
-                final Integer len = ((DecimalSignatureConfiguration)configuration).getLength();
+            case DECIMAL -> {
+                final Integer len = ((DecimalSignatureConfiguration) configuration).getLength();
                 return computePowerAuthDecimalSignature(data, signatureKeys, ctrData, len);
             }
-            default: {
+            default ->
                 throw new GenericCryptoException("Unsupported format of PowerAuth signature.");
-            }
         }
     }
 
diff --git a/powerauth-java-http/src/main/java/io/getlime/security/powerauth/http/validator/ValueTypeValidator.java b/powerauth-java-http/src/main/java/io/getlime/security/powerauth/http/validator/ValueTypeValidator.java
index 9db85bc1..6ec80ba5 100644
--- a/powerauth-java-http/src/main/java/io/getlime/security/powerauth/http/validator/ValueTypeValidator.java
+++ b/powerauth-java-http/src/main/java/io/getlime/security/powerauth/http/validator/ValueTypeValidator.java
@@ -135,21 +135,20 @@ public class ValueTypeValidator {
     public static boolean isValidSignatureValue(String signature) {
         if (signature != null) {
             switch (signature.length()) {
-                case 8:
-                case 17:
-                case 26:
+                case 8, 17, 26 -> {
                     // "2.0", "2.1", "3.0" signature version uses "DECIMAL" format
                     return signature.matches(SIGNATURE_REGEX);
-                case 24:
-                case 44:
-                case 64:
+                }
+                case 24, 44, 64 -> {
                     // "3.1" and later signatures uses "BASE64" format.
                     // We don't need to validate an exact number of encoded bytes. This is due to fact,
                     // that if input string length can only be 24, 44 or 64, then the encoded output length
                     // must be 16, 32 or 48.
                     return isValidBase64OfLengthRange(signature, 16, 48);
-                default:
-                    break;
+                }
+                default -> {
+                    return false;
+                }
             }
         }
         return false;


Test patch:
diff --git a/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/Argon2Test.java b/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/Argon2Test.java
index 1ff0eee0..6d73c70e 100644
--- a/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/Argon2Test.java
+++ b/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/Argon2Test.java
@@ -53,30 +53,26 @@ public class Argon2Test {
 
     @Test
     public void testInvalidHash1() {
-        assertThrows(IOException.class, () -> {
-            Argon2Hash.parse("argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA");
-        });
+        assertThrows(IOException.class, () ->
+            Argon2Hash.parse("argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA"));
     }
 
     @Test
     public void testInvalidHash2() {
-        assertThrows(IOException.class, () -> {
-            Argon2Hash.parse("$argon2i$v=19$c29tZXNhbHQ$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA");
-        });
+        assertThrows(IOException.class, () ->
+            Argon2Hash.parse("$argon2i$v=19$c29tZXNhbHQ$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA"));
     }
 
     @Test
     public void testInvalidHash3() {
-        assertThrows(IOException.class, () -> {
-            Argon2Hash.parse("argon2i$v=19$m=65536,t=2,p=1$$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA");
-        });
+        assertThrows(IOException.class, () ->
+            Argon2Hash.parse("argon2i$v=19$m=65536,t=2,p=1$$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA"));
     }
 
     @Test
     public void testInvalidHash4() {
-        assertThrows(IOException.class, () -> {
-            Argon2Hash.parse("$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$");
-        });
+        assertThrows(IOException.class, () ->
+            Argon2Hash.parse("$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$"));
     }
 
     @Test
diff --git a/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/model/TestVector.java b/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/model/TestVector.java
index 6b7a70ba..715fb992 100644
--- a/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/model/TestVector.java
+++ b/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/lib/util/model/TestVector.java
@@ -19,37 +19,9 @@ package io.getlime.security.powerauth.crypto.lib.util.model;
 import java.util.Map;
 
 /**
+ * @param input Test vector input data.
+ * @param output Test vector output data.
  * Class representing a single test vector.
  */
-public class TestVector {
-
-    private final Map<String, String> input;
-    private final Map<String, String> output;
-
-    /**
-     * Test vector constructor.
-     * @param input Test vector input data.
-     * @param output Test vector output data.
-     */
-    public TestVector(Map<String, String> input, Map<String, String> output) {
-        this.input = input;
-        this.output = output;
-    }
-
-    /**
-     * Get test vector input data.
-     * @return Test vector input data.
-     */
-    public Map<String, String> getInput() {
-        return input;
-    }
-
-    /**
-     * Get test vector output data.
-     * @return Test vector output data.
-     */
-    public Map<String, String> getOutput() {
-        return output;
-    }
-
-}
\ No newline at end of file
+public record TestVector(Map<String, String> input, Map<String, String> output) {
+}
diff --git a/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/signature/PowerAuthSignatureFormatTest.java b/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/signature/PowerAuthSignatureFormatTest.java
index 28c05259..c8d684ff 100644
--- a/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/signature/PowerAuthSignatureFormatTest.java
+++ b/powerauth-java-crypto/src/test/java/io/getlime/security/powerauth/crypto/signature/PowerAuthSignatureFormatTest.java
@@ -41,44 +41,38 @@ public class PowerAuthSignatureFormatTest {
 
     @Test
     public void testInvalidV2() {
-        assertThrows(GenericCryptoException.class, () -> {
-            PowerAuthSignatureFormat.getFormatForSignatureVersion("2.2");
-        });
+        assertThrows(GenericCryptoException.class, () ->
+            PowerAuthSignatureFormat.getFormatForSignatureVersion("2.2"));
     }
 
     @Test
     public void testInvalidV3() {
-        assertThrows(GenericCryptoException.class, () -> {
-            PowerAuthSignatureFormat.getFormatForSignatureVersion("3.05");
-        });
+        assertThrows(GenericCryptoException.class, () ->
+            PowerAuthSignatureFormat.getFormatForSignatureVersion("3.05"));
     }
 
     @Test
     public void testInvalidFormat1() {
-        assertThrows(GenericCryptoException.class, () -> {
-            PowerAuthSignatureFormat.getFormatForSignatureVersion("3.1.1");
-        });
+        assertThrows(GenericCryptoException.class, () ->
+            PowerAuthSignatureFormat.getFormatForSignatureVersion("3.1.1"));
     }
 
     @Test
     public void testInvalidFormat2() {
-        assertThrows(GenericCryptoException.class, () -> {
-            PowerAuthSignatureFormat.getFormatForSignatureVersion("foo");
-        });
+        assertThrows(GenericCryptoException.class, () ->
+            PowerAuthSignatureFormat.getFormatForSignatureVersion("foo"));
     }
 
     @Test
     public void testInvalidFormat3() {
-        assertThrows(GenericCryptoException.class, () -> {
-            PowerAuthSignatureFormat.getFormatForSignatureVersion("");
-        });
+        assertThrows(GenericCryptoException.class, () ->
+            PowerAuthSignatureFormat.getFormatForSignatureVersion(""));
     }
 
     @Test
     public void testInvalidFormat4() {
-        assertThrows(GenericCryptoException.class, () -> {
-            PowerAuthSignatureFormat.getFormatForSignatureVersion(null);
-        });
+        assertThrows(GenericCryptoException.class, () ->
+            PowerAuthSignatureFormat.getFormatForSignatureVersion(null));
     }
 
     @Test