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 static org.kuali.common.util.base.Exceptions.illegalArgument; 019import static org.kuali.common.util.base.Precondition.checkNotBlank; 020import static org.kuali.common.util.base.Precondition.checkNotNull; 021import static org.kuali.common.util.enc.EncStrength.DEFAULT_ENCRYPTION_STRENGTH; 022 023import java.util.ArrayList; 024import java.util.List; 025 026import org.jasypt.util.text.BasicTextEncryptor; 027import org.jasypt.util.text.StrongTextEncryptor; 028import org.jasypt.util.text.TextEncryptor; 029import org.kuali.common.util.Assert; 030import org.kuali.common.util.Str; 031 032import com.google.common.base.Optional; 033import com.google.common.collect.ImmutableList; 034 035/** 036 * @deprecated 037 */ 038@Deprecated 039public class EncUtils { 040 041 private static final String MAGIC_PREFIX = "enc--"; // Handy for passing encrypted args via CLI. Parenthesis tend to confuse CLI shells 042 private static final String PREFIX = "ENC("; // Jasypt prefers this for values in properties files 043 private static final String SUFFIX = ")"; 044 045 public static final Optional<EncryptionService> ABSENT = Optional.absent(); 046 047 /** 048 * Return true if the text is enclosed with <code>ENC()</code> or starts with <code>enc--</code> 049 */ 050 public static boolean isEncrypted(String text) { 051 if (text == null) { 052 return false; 053 } 054 return Str.matches(text, PREFIX, SUFFIX) || text.startsWith(MAGIC_PREFIX); 055 } 056 057 public static String unwrap(String text) { 058 Assert.noBlanks(text); 059 Assert.encrypted(text); 060 if (text.startsWith(MAGIC_PREFIX)) { 061 return Str.removePrefix(text, MAGIC_PREFIX); 062 } else { 063 return Str.remove(text, PREFIX, SUFFIX); 064 } 065 } 066 067 public static String wrap(String text) { 068 Assert.noBlanks(text); 069 Assert.notEncrypted(text); 070 return MAGIC_PREFIX + text; 071 } 072 073 /** 074 * Returns a <code>BasicTextEncryptor</code> that uses <code>password</code> to encrypt/decrypt. 075 */ 076 public static TextEncryptor getTextEncryptor(String password) { 077 return getTextEncryptor(password, DEFAULT_ENCRYPTION_STRENGTH); 078 } 079 080 /** 081 * Return a <code>BasicTextEncryptor</code> or <code>StrongTextEncryptor</code> depending on what <code>strength</code> is set to 082 */ 083 public static TextEncryptor getTextEncryptor(String password, EncStrength strength) { 084 checkNotBlank(password, "password"); 085 checkNotNull(strength, "strength"); 086 switch (strength) { 087 case BASIC: 088 BasicTextEncryptor basic = new BasicTextEncryptor(); 089 basic.setPassword(password); 090 return basic; 091 case STRONG: 092 StrongTextEncryptor strong = new StrongTextEncryptor(); 093 strong.setPassword(password); 094 return strong; 095 default: 096 throw illegalArgument("encryption strength [%s] is unknown", strength); 097 } 098 } 099 100 /** 101 * If enc and string are both present and the string is encrypted, return the decrypted string. Otherwise do nothing. 102 */ 103 public static Optional<String> decrypt(Optional<EncryptionService> enc, Optional<String> string) { 104 if (enc.isPresent() && string.isPresent()) { 105 return Optional.of(enc.get().decrypt(string.get())); 106 } else { 107 return string; 108 } 109 } 110 111 /** 112 * If enc is present and the string is encrypted, return the decrypted string. Otherwise do nothing. 113 */ 114 public static String decrypt(Optional<EncryptionService> enc, String string) { 115 return decrypt(enc, Optional.of(string)).get(); 116 } 117 118 /** 119 * If enc is present, return a new list containing the same elements in the same order only with any encrypted strings having been decrypted. 120 */ 121 public static List<String> decrypt(Optional<EncryptionService> enc, List<String> strings) { 122 if (!enc.isPresent()) { 123 return strings; 124 } 125 List<String> decrypted = new ArrayList<String>(); 126 for (String string : strings) { 127 decrypted.add(enc.get().decrypt(string)); 128 } 129 return ImmutableList.copyOf(decrypted); 130 } 131 132 /** 133 * Return a new list containing the same elements in the same order only with any encrypted strings having been decrypted. 134 */ 135 public static List<String> decrypt(EncryptionService enc, List<String> strings) { 136 return decrypt(Optional.of(enc), strings); 137 } 138 139 public static Optional<String> decrypt(EncryptionService enc, Optional<String> optional) { 140 return decrypt(Optional.of(enc), optional); 141 } 142 143}