Created
November 24, 2008 16:28
-
-
Save riferrei/28515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Retention(value=RetentionPolicy.RUNTIME) | |
@Target(value=ElementType.FIELD) | |
public @interface CipherData { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CipherEntityListener { | |
@PrePersist @PreUpdate | |
@SuppressWarnings("unchecked") | |
public void cipherEntityUsingMD5(Object entity) throws Exception { | |
CipherHelper cipherHelper = null; | |
String privateKey = null; | |
String recordData = null; | |
StringBuffer sb = null; | |
Class classType = null; | |
Field[] fieldList = null; | |
Object fieldValue = null; | |
String sValue = null; | |
boolean keyHasBeenSet = false; | |
boolean dataHasBeenSet = false; | |
try { | |
sb = new StringBuffer(); | |
classType = entity.getClass(); | |
fieldList = classType.getDeclaredFields(); | |
for (Field field : fieldList) { | |
field.setAccessible(true); | |
fieldValue = field.get(entity); | |
if (fieldValue != null) { | |
sValue = fieldValue.toString(); | |
sb.append(sValue); | |
} | |
} | |
cipherHelper = CipherHelper.getInstance(); | |
privateKey = cipherHelper.generatePrivateKey(); | |
recordData = cipherHelper.encryptDataUsingMD5(sb.toString(), | |
privateKey); | |
for (Field field : fieldList) { | |
if (field.isAnnotationPresent(CipherKey.class)) { | |
field.set(entity, privateKey); | |
keyHasBeenSet = true; | |
} | |
if (field.isAnnotationPresent(CipherData.class)) { | |
field.set(entity, recordData); | |
dataHasBeenSet = true; | |
} | |
if (keyHasBeenSet && dataHasBeenSet) { | |
break; | |
} | |
} | |
} catch (Exception ex) { | |
throw ex; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Retention(value=RetentionPolicy.RUNTIME) | |
@Target(value=ElementType.FIELD) | |
public @interface CipherKey { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name="tab_cliente") | |
@SuppressWarnings("serial") | |
@EntityListeners(CipherEntityListener.class) | |
public class Cliente implements Serializable { | |
@Id | |
@Column(name="cod_cliente") | |
@GeneratedValue(strategy=GenerationType.IDENTITY) | |
private int codigo; | |
@Column(name="nome_cliente", length=100) | |
private String nome; | |
@Column(name="cpf_cliente", length=20) | |
private String cpf; | |
@Column(name="data_nasc") | |
@Temporal(TemporalType.DATE) | |
private Date dataNascimento; | |
@CipherKey | |
private String privateKey; | |
@CipherData | |
private String recordData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment