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;
017
018import static java.lang.String.format;
019import static org.kuali.common.util.encrypt.openssl.OpenSSL.buildOpenSSLEncryptor;
020import static org.kuali.common.util.log.Loggers.newLogger;
021
022import org.kuali.common.util.encrypt.provider.DefaultEncryptionContextProviderChain;
023import org.kuali.common.util.encrypt.provider.EncryptionContextProvider;
024import org.slf4j.Logger;
025
026import com.google.common.base.Optional;
027
028public final class Encryption {
029
030        private static final Logger logger = newLogger();
031
032        private static Encryptor encryptor;
033
034        public static Encryptor buildEncryptor() {
035                EncryptionContextProvider provider = new DefaultEncryptionContextProviderChain();
036                Optional<EncryptionContext> context = provider.getEncryptionContext();
037                if (context.isPresent()) {
038                        return buildOpenSSLEncryptor(context.get());
039                } else {
040                        logger.info(format("encryption disabled. no encryption password was located"));
041                        return NoOpEncryptor.INSTANCE;
042                }
043        }
044
045        public synchronized static Encryptor getDefaultEncryptor() {
046                if (encryptor == null) {
047                        encryptor = buildEncryptor();
048                }
049                return encryptor;
050        }
051
052}