I try to use database encryption for single fields with Spring and Jpa (Hibernate). Here is the part of the Entity:
@ColumnTransformer(
read="AES_DECRYPT(UNHEX(lastname), UNHEX(SHA2('secret', 512)))",
write="HEX(AES_ENCRYPT(?, UNHEX(SHA2('secret', 512))))"
)
private String lastname;
This uses Mysql-functions to encrypt and decrypt my field, so I do not have to care in Java.
My problem is that I cannot hardcode the passphrase in my Java-Code, but Java-Annotations only allow non-dynamic final Strings as params. How do I use a spring application property to replace the passphrase 'secret'
?
I cannot use a Jpa-Converter, because I want to be able to filter and sort by lastname
. I also tried to subclass MySQL5InnoDBDialect and register a StandardSQLFunction
, but that does not work conceptually with @ColumnTransformer
because these functions are registered in the context of JPA, not SQL. I also thought of programmatically manipulating the hibernate config before it is used to create the EntityManagerFactory, but I do not know how to do that. Any help appreciated.