Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. public class CipherEntityListener {
  2.  
  3. @PrePersist @PreUpdate
  4. @SuppressWarnings("unchecked")
  5. public void cipherEntityUsingMD5(Object entity) throws Exception {
  6. CipherHelper cipherHelper = null;
  7. String privateKey = null;
  8. String recordData = null;
  9. StringBuffer sb = null;
  10. Class classType = null;
  11. Field[] fieldList = null;
  12. Object fieldValue = null;
  13. String sValue = null;
  14. boolean keyHasBeenSet = false;
  15. boolean dataHasBeenSet = false;
  16. try {
  17. sb = new StringBuffer();
  18. classType = entity.getClass();
  19. fieldList = classType.getDeclaredFields();
  20. for (Field field : fieldList) {
  21. field.setAccessible(true);
  22. fieldValue = field.get(entity);
  23. if (fieldValue != null) {
  24. sValue = fieldValue.toString();
  25. sb.append(sValue);
  26. }
  27. }
  28. cipherHelper = CipherHelper.getInstance();
  29. privateKey = cipherHelper.generatePrivateKey();
  30. recordData = cipherHelper.encryptDataUsingMD5(sb.toString(),
  31. privateKey);
  32. for (Field field : fieldList) {
  33. if (field.isAnnotationPresent(CipherKey.class)) {
  34. field.set(entity, privateKey);
  35. keyHasBeenSet = true;
  36. }
  37. if (field.isAnnotationPresent(CipherData.class)) {
  38. field.set(entity, recordData);
  39. dataHasBeenSet = true;
  40. }
  41. if (keyHasBeenSet && dataHasBeenSet) {
  42. break;
  43. }
  44. }
  45. } catch (Exception ex) {
  46. throw ex;
  47. }
  48. }
  49.  
  50. }
Add Comment
Please, Sign In to add comment