Skip to content

Commit

Permalink
Throw Exception on db decryption/encryption issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonononoki committed Dec 17, 2024
1 parent b17a915 commit 65f6579
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.nonononoki.alovoa.model.DatabaseRuntimeException;
import jakarta.persistence.AttributeConverter;

import org.springframework.beans.factory.annotation.Value;
Expand All @@ -36,21 +38,21 @@ public class TextEncryptorConverter implements AttributeConverter<String, String

SecureRandom random = new SecureRandom();

private IvParameterSpec getIvSpec() {
private synchronized IvParameterSpec getIvSpec() {
if (ivSpec == null) {
ivSpec = new IvParameterSpec(salt.getBytes(StandardCharsets.UTF_8));
}
return ivSpec;
}

private SecretKeySpec getKeySpec() {
private synchronized SecretKeySpec getKeySpec() {
if (keySpec == null) {
keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
}
return keySpec;
}

private Cipher getEnCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
private synchronized Cipher getEnCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException {
if (enCipher == null) {
enCipher = Cipher.getInstance(TRANSFORMATION);
Expand All @@ -59,7 +61,7 @@ private Cipher getEnCipher() throws NoSuchAlgorithmException, NoSuchPaddingExcep
return enCipher;
}

private Cipher getDeCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
private synchronized Cipher getDeCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException {
if (deCipher == null) {
deCipher = Cipher.getInstance(TRANSFORMATION);
Expand All @@ -86,20 +88,20 @@ public String decode(String dbData)
}

@Override
public String convertToDatabaseColumn(String attribute) {
public String convertToDatabaseColumn(String attribute) throws DatabaseRuntimeException {
try {
return encode(attribute);
} catch (Exception e) {
return attribute;
throw new DatabaseRuntimeException(e);
}
}

@Override
public String convertToEntityAttribute(String dbData) {
public String convertToEntityAttribute(String dbData) throws DatabaseRuntimeException {
try {
return decode(dbData);
} catch (Exception e) {
return dbData;
throw new DatabaseRuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nonononoki.alovoa.model;

public class DatabaseRuntimeException extends RuntimeException {

public DatabaseRuntimeException(Exception e) {
super(e);
}
}

0 comments on commit 65f6579

Please sign in to comment.