1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.jpa.converters;
17
18 import javax.persistence.AttributeConverter;
19 import javax.persistence.Converter;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.rice.core.api.CoreApiServiceLocator;
23 import org.kuali.rice.core.api.encryption.EncryptionService;
24
25
26
27
28
29
30 @Converter
31 public class HashConverter implements AttributeConverter<String, String> {
32
33
34
35
36
37
38 @Override
39 public String convertToDatabaseColumn(String objectValue) {
40
41 if (objectValue == null) {
42 return null;
43 }
44 if (StringUtils.isEmpty(objectValue.toString())) {
45 return "";
46 }
47
48 if (objectValue.toString().endsWith(EncryptionService.HASH_POST_PREFIX)) {
49 return StringUtils.stripEnd(objectValue.toString(), EncryptionService.HASH_POST_PREFIX);
50 } else {
51 try {
52 return CoreApiServiceLocator.getEncryptionService().hash(objectValue);
53 } catch (Exception e) {
54 throw new RuntimeException("Exception while attempting to hash value for DB: ", e);
55 }
56 }
57 }
58
59
60
61
62
63
64 @Override
65 public String convertToEntityAttribute(String dataValue) {
66
67 if (dataValue == null) {
68 return null;
69 }
70 if (StringUtils.isEmpty(dataValue.toString())) {
71 return "";
72 }
73 return dataValue.toString() + EncryptionService.HASH_POST_PREFIX;
74 }
75
76 }