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.encrypt.jasypt;
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;
021
022import org.jasypt.util.text.BasicTextEncryptor;
023import org.jasypt.util.text.StrongTextEncryptor;
024import org.jasypt.util.text.TextEncryptor;
025import org.kuali.common.util.encrypt.EncryptionContext;
026import org.kuali.common.util.encrypt.EncryptionStrength;
027
028public class Jasypt {
029
030        /**
031         * Return a <code>BasicTextEncryptor</code> or <code>StrongTextEncryptor</code> depending on what <code>strength</code> is set to
032         */
033        public static TextEncryptor buildTextEncryptor(EncryptionContext context) {
034                return buildTextEncryptor(context.getPassword(), context.getStrength());
035        }
036
037        /**
038         * Return a <code>BasicTextEncryptor</code> or <code>StrongTextEncryptor</code> depending on what <code>strength</code> is set to
039         */
040        public static TextEncryptor buildTextEncryptor(String password, EncryptionStrength strength) {
041                checkNotBlank(password, "password");
042                checkNotNull(strength, "strength");
043                switch (strength) {
044                case BASIC_ENCRYPTION_STRENGTH:
045                        BasicTextEncryptor basic = new BasicTextEncryptor();
046                        basic.setPassword(password);
047                        return basic;
048                case STRONG_ENCRYPTION_STRENGTH:
049                        StrongTextEncryptor strong = new StrongTextEncryptor();
050                        strong.setPassword(password);
051                        return strong;
052                default:
053                        throw illegalArgument("encryption strength [%s] is unknown", strength);
054                }
055        }
056
057}