001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.data.jpa.converters;
017
018import javax.persistence.AttributeConverter;
019import javax.persistence.Converter;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.rice.core.api.CoreApiServiceLocator;
023import org.kuali.rice.core.api.encryption.EncryptionService;
024
025/**
026 * Calls the core service to hash values going to the database.
027 * 
028 *  @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030@Converter
031public class HashConverter implements AttributeConverter<String, String> {
032
033    /**
034     * {@inheritDoc}
035     *
036     * This implementation hashes the value going to the database.
037     */
038        @Override
039        public String convertToDatabaseColumn(String objectValue) {
040                // don't attempt to encrypt nulls or empty strings
041                if (objectValue == null) {
042                        return null;
043                }
044                if (StringUtils.isEmpty(objectValue.toString())) {
045                        return "";
046                }
047                // don't convert if already a hashed value
048                if (objectValue.toString().endsWith(EncryptionService.HASH_POST_PREFIX)) {
049                        return StringUtils.stripEnd(objectValue.toString(), EncryptionService.HASH_POST_PREFIX);
050                } else {
051                        try {
052                                return CoreApiServiceLocator.getEncryptionService().hash(objectValue);
053                        } catch (Exception e) {
054                                throw new RuntimeException("Exception while attempting to hash value for DB: ", e);
055                        }
056                }
057        }
058
059    /**
060     * {@inheritDoc}
061     *
062     * This implementation directly returns the hash value coming from the database.
063     */
064        @Override
065        public String convertToEntityAttribute(String dataValue) {
066                // don't attempt to decrypt nulls or empty strings
067                if (dataValue == null) {
068                        return null;
069                }
070                if (StringUtils.isEmpty(dataValue.toString())) {
071                        return "";
072                }
073                return dataValue.toString() + EncryptionService.HASH_POST_PREFIX;
074        }
075
076}