View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.enc;
17  
18  import java.util.List;
19  import java.util.Properties;
20  import java.util.SortedSet;
21  
22  import org.jasypt.util.text.TextEncryptor;
23  import org.kuali.common.util.Assert;
24  import org.kuali.common.util.PropertyUtils;
25  
26  import com.google.common.collect.Sets;
27  
28  /**
29   * @deprecated
30   */
31  @Deprecated
32  public final class DefaultEncryptionService implements EncryptionService {
33  
34  	private final TextEncryptor encryptor;
35  
36  	public DefaultEncryptionService(TextEncryptor encryptor) {
37  		Assert.noNulls(encryptor);
38  		this.encryptor = encryptor;
39  	}
40  
41  	@Override
42  	public String encrypt(String text) {
43  		if (EncUtils.isEncrypted(text)) {
44  			return text; // It's already encrypted, just return it
45  		}
46  		Assert.notEncrypted(text);
47  		String encryptedText = encryptor.encrypt(text);
48  		return EncUtils.wrap(encryptedText);
49  	}
50  
51  	@Override
52  	public String decrypt(String text) {
53  		if (!EncUtils.isEncrypted(text)) {
54  			return text; // It's not encrypted, just return it
55  		}
56  		Assert.encrypted(text);
57  		String unwrapped = EncUtils.unwrap(text);
58  		return encryptor.decrypt(unwrapped);
59  	}
60  
61  	/**
62  	 * Decrypt any encrypted property values.
63  	 */
64  	@Override
65  	public void decrypt(Properties properties) {
66  		List<String> keys = PropertyUtils.getEncryptedKeys(properties);
67  		for (String key : keys) {
68  			String encrypted = properties.getProperty(key);
69  			String decrypted = decrypt(encrypted);
70  			properties.setProperty(key, decrypted);
71  		}
72  	}
73  
74  	/**
75  	 * Encrypt any property values that are not already encrypted.
76  	 */
77  	@Override
78  	public void encrypt(Properties properties) {
79  		SortedSet<String> allKeys = Sets.newTreeSet(properties.stringPropertyNames());
80  		SortedSet<String> encKeys = Sets.newTreeSet(PropertyUtils.getEncryptedKeys(properties));
81  		SortedSet<String> keys = Sets.newTreeSet(Sets.difference(allKeys, encKeys));
82  		for (String key : keys) {
83  			String plaintext = properties.getProperty(key);
84  			String encrypted = encrypt(plaintext);
85  			properties.setProperty(key, encrypted);
86  		}
87  	}
88  
89  	public TextEncryptor getEncryptor() {
90  		return encryptor;
91  	}
92  
93  }