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 @Override
34 public String convertToDatabaseColumn(String objectValue) {
35
36 if (objectValue == null) {
37 return null;
38 }
39 if (StringUtils.isEmpty(objectValue.toString())) {
40 return "";
41 }
42
43 if (objectValue.toString().endsWith(EncryptionService.HASH_POST_PREFIX)) {
44 return StringUtils.stripEnd(objectValue.toString(), EncryptionService.HASH_POST_PREFIX);
45 } else {
46 try {
47 return CoreApiServiceLocator.getEncryptionService().hash(objectValue);
48 } catch (Exception e) {
49 throw new RuntimeException("Exception while attempting to hash value for DB: ", e);
50 }
51 }
52 }
53
54 @Override
55 public String convertToEntityAttribute(String dataValue) {
56
57 if (dataValue == null) {
58 return null;
59 }
60 if (StringUtils.isEmpty(dataValue.toString())) {
61 return "";
62 }
63 return dataValue.toString() + EncryptionService.HASH_POST_PREFIX;
64 }
65
66 }