I have stored my private.key and public.key in resources folder and I have loaded it using
` public static String loadPrivateKey() throws IOException {
Resource privateKeyResource = new ClassPathResource("private.key");
return readInputStream(privateKeyResource.getInputStream());
}
private static String readInputStream(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
}
return stringBuilder.toString();`
But when I try to inject it into @ColumnTransformer, it gives the error java: element value must be a constant expression
.
Below is my implementation part related to this in my entity class.
` private static final String PUBLIC_KEY;
private static final String PRIVATE_KEY;
static {
try {
PUBLIC_KEY = KeyLoader.loadPublicKey();
PRIVATE_KEY = KeyLoader.loadPrivateKey();
} catch (IOException e) {
throw new RuntimeException("Failed to load keys", e);
}
}
private static final String readEx="pgp_pub_decrypt(asymdata, dearmor('" + PRIVATE_KEY + "'),'passphrase')";
private static final String writeEx= "pgp_pub_encrypt(?, dearmor('"+PUBLIC_KEY+"'))";
@Column(name="asymdata", columnDefinition = "bytea")
@ColumnTransformer(
read = readEx,
write = writeEx)
private String asymdata;`
What I need is a way to get my private key from private.key file and pass it to ColumnTransformer annotation.
New contributor
Kavindu Kalinga is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.