From 7948d210377cc7c36f906893d890f11613257a65 Mon Sep 17 00:00:00 2001 From: Martin Vogel <martin.vogel@sinc.de> Date: Wed, 22 Jun 2022 18:35:08 +0200 Subject: [PATCH] #414 Update moved exception location --- .../core/crypto/JWECryptoService.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/de/fitko/fitconnect/core/crypto/JWECryptoService.java b/core/src/main/java/de/fitko/fitconnect/core/crypto/JWECryptoService.java index daf6468bc..82cf7606e 100644 --- a/core/src/main/java/de/fitko/fitconnect/core/crypto/JWECryptoService.java +++ b/core/src/main/java/de/fitko/fitconnect/core/crypto/JWECryptoService.java @@ -5,8 +5,8 @@ import com.nimbusds.jose.crypto.RSADecrypter; import com.nimbusds.jose.crypto.RSAEncrypter; import com.nimbusds.jose.jwk.RSAKey; import de.fitko.fitconnect.api.services.crypto.CryptoService; -import de.fitko.fitconnect.api.exceptions.internal.DecryptionException; -import de.fitko.fitconnect.api.exceptions.internal.EncryptionException; +import de.fitko.fitconnect.api.exceptions.DecryptionException; +import de.fitko.fitconnect.api.exceptions.EncryptionException; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; @@ -20,54 +20,54 @@ public class JWECryptoService implements CryptoService { public static final EncryptionMethod ENCRYPTION_METHOD = EncryptionMethod.A256GCM; @Override - public String decryptString(RSAKey privateKey, String encryptedData) throws DecryptionException { + public String decryptString(final RSAKey privateKey, final String encryptedData) throws DecryptionException { return decrypt(privateKey, encryptedData).toString(); } @Override - public byte[] decryptBytes(RSAKey privateKey, String encryptedData) throws DecryptionException { + public byte[] decryptBytes(final RSAKey privateKey, final String encryptedData) throws DecryptionException { return decrypt(privateKey, encryptedData).toBytes(); } @Override - public String encryptString(RSAKey publicKey, String data) throws EncryptionException { + public String encryptString(final RSAKey publicKey, final String data) throws EncryptionException { final Payload payload = new Payload(data); return encrypt(publicKey, payload); } @Override - public String encryptBytes(RSAKey publicKey, byte[] bytes) throws EncryptionException { - final Payload payload = new Payload(bytes); + public String encryptBytes(final RSAKey publicKey, final byte[] bytes) throws EncryptionException { + final Payload payload = new Payload(bytes); return encrypt(publicKey, payload); } - private String encrypt(RSAKey publicKey, Payload payload) throws EncryptionException { + private String encrypt(final RSAKey publicKey, final Payload payload) throws EncryptionException { try { - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + final KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(ENCRYPTION_METHOD.cekBitLength()); final SecretKey cek = keyGenerator.generateKey(); final String keyID = getIdFromPublicKey(publicKey); - return encryptPayload(publicKey,payload, cek, keyID); - } catch (NoSuchAlgorithmException | JOSEException e) { + return encryptPayload(publicKey, payload, cek, keyID); + } catch (final NoSuchAlgorithmException | JOSEException e) { throw new EncryptionException(e.getMessage(), e); } } - private Payload decrypt(RSAKey privateKey, String encData) throws DecryptionException { + private Payload decrypt(final RSAKey privateKey, final String encData) throws DecryptionException { try { final JWEObject jwe = JWEObject.parse(encData); jwe.decrypt(new RSADecrypter(privateKey)); return jwe.getPayload(); - } catch (ParseException | JOSEException e) { + } catch (final ParseException | JOSEException e) { throw new DecryptionException(e.getMessage(), e); } } - private JWEObject getJWEObject(String keyID, Payload payload) { + private JWEObject getJWEObject(final String keyID, final Payload payload) { return new JWEObject(getJWEHeader(keyID), payload); } - private JWEHeader getJWEHeader(String keyID) { + private JWEHeader getJWEHeader(final String keyID) { return new JWEHeader.Builder(ALGORITHM, ENCRYPTION_METHOD) .compressionAlgorithm(CompressionAlgorithm.DEF) .contentType("application/json") @@ -75,24 +75,24 @@ public class JWECryptoService implements CryptoService { .build(); } - private RSAEncrypter getEncrypter(RSAPublicKey publicKey, SecretKey cek) { + private RSAEncrypter getEncrypter(final RSAPublicKey publicKey, final SecretKey cek) { return new RSAEncrypter(publicKey, cek); } - private String encryptPayload(RSAKey publicKey, Payload payload, SecretKey cek, String keyID) throws JOSEException, EncryptionException { - JWEObject jwe = getJWEObject(keyID, payload); + private String encryptPayload(final RSAKey publicKey, final Payload payload, final SecretKey cek, final String keyID) throws JOSEException, EncryptionException { + final JWEObject jwe = getJWEObject(keyID, payload); jwe.encrypt(getEncrypter(publicKey.toRSAPublicKey(), cek)); checkIfJWEObjectIsEncrypted(jwe); return jwe.serialize(); } - private void checkIfJWEObjectIsEncrypted(JWEObject jwe) throws EncryptionException { + private void checkIfJWEObjectIsEncrypted(final JWEObject jwe) throws EncryptionException { if (!jwe.getState().equals(JWEObject.State.ENCRYPTED)) { throw new EncryptionException("JWE object is not encrypted"); } } - private String getIdFromPublicKey(RSAKey publicKey) throws EncryptionException { + private String getIdFromPublicKey(final RSAKey publicKey) throws EncryptionException { final String keyID = publicKey.getKeyID(); if (keyID == null || keyID.isEmpty()) { throw new EncryptionException("public key has no keyID"); -- GitLab