|
| 1 | +package fr.yodamad.svn2git.security; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.beans.factory.annotation.Configurable; |
| 6 | +import org.springframework.core.env.Environment; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | + |
| 9 | +import javax.crypto.BadPaddingException; |
| 10 | +import javax.crypto.Cipher; |
| 11 | +import javax.crypto.IllegalBlockSizeException; |
| 12 | +import javax.crypto.NoSuchPaddingException; |
| 13 | +import javax.persistence.AttributeConverter; |
| 14 | +import javax.persistence.Converter; |
| 15 | +import java.security.InvalidAlgorithmParameterException; |
| 16 | +import java.security.InvalidKeyException; |
| 17 | +import java.security.NoSuchAlgorithmException; |
| 18 | +import java.util.Base64; |
| 19 | + |
| 20 | +/** |
| 21 | + * Concrete implementation of the {@link AttributeConverter} |
| 22 | + * abstract class to encrypt/decrypt an entity attribute of type |
| 23 | + * {@link java.lang.String} <br/> |
| 24 | + * Note: This is the class where the {@literal @}Converter annotation is applied |
| 25 | + * |
| 26 | + * @author Sunit Katkar, sunitkatkar@gmail.com |
| 27 | + * @since ver 1.0 (Apr 2018) |
| 28 | + * @version 1.0 * |
| 29 | + */ |
| 30 | +@Component |
| 31 | +@Configurable |
| 32 | +@Converter(autoApply = false) |
| 33 | +public class PasswordConverter implements AttributeConverter<String, String> { |
| 34 | + |
| 35 | + /** Spring env to retrieve secret. */ |
| 36 | + private static Environment env; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + public void initEnv(Environment en){ |
| 40 | + PasswordConverter.env = en; |
| 41 | + } |
| 42 | + |
| 43 | + /** CipherMaker is needed to configure and create instance of Cipher */ |
| 44 | + private CipherMaker cipherMaker; |
| 45 | + |
| 46 | + /** |
| 47 | + * Default constructor initializes with an instance of the |
| 48 | + * {@link CipherMaker} crypto class to get a {@link javax.crypto.Cipher} |
| 49 | + * instance |
| 50 | + */ |
| 51 | + public PasswordConverter() { |
| 52 | + this(new CipherMaker()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructor |
| 57 | + * |
| 58 | + * @param cipherMaker |
| 59 | + */ |
| 60 | + public PasswordConverter(CipherMaker cipherMaker) { |
| 61 | + this.cipherMaker = cipherMaker; |
| 62 | + } |
| 63 | + |
| 64 | + /* |
| 65 | + * (non-Javadoc) |
| 66 | + * |
| 67 | + * @see |
| 68 | + * javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang. |
| 69 | + * Object) |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public String convertToDatabaseColumn(String attribute) { |
| 73 | + String secret = env.getProperty("password.cipher"); |
| 74 | + if (!StringUtils.isEmpty(secret) && !StringUtils.isEmpty(attribute)) { |
| 75 | + try { |
| 76 | + Cipher cipher = cipherMaker.configureAndGetInstance(Cipher.ENCRYPT_MODE, secret); |
| 77 | + return encryptData(cipher, attribute); |
| 78 | + } catch (NoSuchAlgorithmException |
| 79 | + | InvalidKeyException |
| 80 | + | InvalidAlgorithmParameterException |
| 81 | + | BadPaddingException |
| 82 | + | NoSuchPaddingException |
| 83 | + | IllegalBlockSizeException e) { |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + return convertEntityAttributeToString(attribute); |
| 88 | + } |
| 89 | + |
| 90 | + /* |
| 91 | + * (non-Javadoc) |
| 92 | + * |
| 93 | + * @see |
| 94 | + * javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang. |
| 95 | + * Object) |
| 96 | + */ |
| 97 | + @Override |
| 98 | + public String convertToEntityAttribute(String dbData) { |
| 99 | + String secret = env.getProperty("password.cipher"); |
| 100 | + if (!StringUtils.isEmpty(secret) && !StringUtils.isEmpty(dbData)) { |
| 101 | + try { |
| 102 | + Cipher cipher = cipherMaker.configureAndGetInstance(Cipher.DECRYPT_MODE, secret); |
| 103 | + return decryptData(cipher, dbData); |
| 104 | + } catch (NoSuchAlgorithmException |
| 105 | + | InvalidAlgorithmParameterException |
| 106 | + | InvalidKeyException |
| 107 | + | BadPaddingException |
| 108 | + | NoSuchPaddingException |
| 109 | + | IllegalBlockSizeException e) { |
| 110 | + throw new RuntimeException(e); |
| 111 | + } |
| 112 | + } |
| 113 | + return convertStringToEntityAttribute(dbData); |
| 114 | + } |
| 115 | + |
| 116 | + private String convertStringToEntityAttribute(String dbData) { |
| 117 | + // the input is a string and output is a string |
| 118 | + return dbData; |
| 119 | + } |
| 120 | + |
| 121 | + private String convertEntityAttributeToString(String attribute) { |
| 122 | + // Here too the input is a string and output is a string |
| 123 | + return attribute; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Helper method to encrypt data |
| 128 | + * |
| 129 | + * @param cipher |
| 130 | + * @param attribute |
| 131 | + * @return |
| 132 | + * @throws IllegalBlockSizeException |
| 133 | + * @throws BadPaddingException |
| 134 | + */ |
| 135 | + private String encryptData(Cipher cipher, String attribute) |
| 136 | + throws IllegalBlockSizeException, BadPaddingException { |
| 137 | + byte[] bytesToEncrypt = convertEntityAttributeToString(attribute).getBytes(); |
| 138 | + byte[] encryptedBytes = cipher.doFinal(bytesToEncrypt); |
| 139 | + return Base64.getEncoder().encodeToString(encryptedBytes); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Helper method to decrypt data |
| 144 | + * |
| 145 | + * @param cipher |
| 146 | + * @param dbData |
| 147 | + * @return |
| 148 | + * @throws IllegalBlockSizeException |
| 149 | + * @throws BadPaddingException |
| 150 | + */ |
| 151 | + private String decryptData(Cipher cipher, String dbData) |
| 152 | + throws IllegalBlockSizeException, BadPaddingException { |
| 153 | + byte[] bytesToDecrypt = Base64.getDecoder().decode(dbData); |
| 154 | + byte[] decryptedBytes = cipher.doFinal(bytesToDecrypt); |
| 155 | + return convertStringToEntityAttribute(new String(decryptedBytes)); |
| 156 | + } |
| 157 | +} |
0 commit comments