001/**
002 * Copyright 2010-2014 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.common.util.enc;
017
018import java.util.List;
019import java.util.Properties;
020import java.util.SortedSet;
021
022import org.jasypt.util.text.TextEncryptor;
023import org.kuali.common.util.Assert;
024import org.kuali.common.util.PropertyUtils;
025
026import com.google.common.collect.Sets;
027
028/**
029 * @deprecated
030 */
031@Deprecated
032public final class DefaultEncryptionService implements EncryptionService {
033
034        private final TextEncryptor encryptor;
035
036        public DefaultEncryptionService(TextEncryptor encryptor) {
037                Assert.noNulls(encryptor);
038                this.encryptor = encryptor;
039        }
040
041        @Override
042        public String encrypt(String text) {
043                if (EncUtils.isEncrypted(text)) {
044                        return text; // It's already encrypted, just return it
045                }
046                Assert.notEncrypted(text);
047                String encryptedText = encryptor.encrypt(text);
048                return EncUtils.wrap(encryptedText);
049        }
050
051        @Override
052        public String decrypt(String text) {
053                if (!EncUtils.isEncrypted(text)) {
054                        return text; // It's not encrypted, just return it
055                }
056                Assert.encrypted(text);
057                String unwrapped = EncUtils.unwrap(text);
058                return encryptor.decrypt(unwrapped);
059        }
060
061        /**
062         * Decrypt any encrypted property values.
063         */
064        @Override
065        public void decrypt(Properties properties) {
066                List<String> keys = PropertyUtils.getEncryptedKeys(properties);
067                for (String key : keys) {
068                        String encrypted = properties.getProperty(key);
069                        String decrypted = decrypt(encrypted);
070                        properties.setProperty(key, decrypted);
071                }
072        }
073
074        /**
075         * Encrypt any property values that are not already encrypted.
076         */
077        @Override
078        public void encrypt(Properties properties) {
079                SortedSet<String> allKeys = Sets.newTreeSet(properties.stringPropertyNames());
080                SortedSet<String> encKeys = Sets.newTreeSet(PropertyUtils.getEncryptedKeys(properties));
081                SortedSet<String> keys = Sets.newTreeSet(Sets.difference(allKeys, encKeys));
082                for (String key : keys) {
083                        String plaintext = properties.getProperty(key);
084                        String encrypted = encrypt(plaintext);
085                        properties.setProperty(key, encrypted);
086                }
087        }
088
089        public TextEncryptor getEncryptor() {
090                return encryptor;
091        }
092
093}