001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.pwd;
016    
017    import com.liferay.portal.exception.PwdEncryptorException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PropsUtil;
023    
024    /**
025     * @author Michael C. Han
026     */
027    public abstract class BasePasswordEncryptor implements PasswordEncryptor {
028    
029            @Override
030            public String encrypt(String plainTextPassword, String encryptedPassword)
031                    throws PwdEncryptorException {
032    
033                    return encrypt(
034                            getDefaultPasswordAlgorithmType(), plainTextPassword,
035                            encryptedPassword);
036            }
037    
038            @Override
039            public String encrypt(
040                            String algorithm, String plainTextPassword,
041                            String encryptedPassword)
042                    throws PwdEncryptorException {
043    
044                    if (Validator.isNull(plainTextPassword)) {
045                            throw new PwdEncryptorException("Unable to encrypt blank password");
046                    }
047    
048                    return doEncrypt(algorithm, plainTextPassword, encryptedPassword);
049            }
050    
051            @Override
052            public String getDefaultPasswordAlgorithmType() {
053                    return _PASSWORDS_ENCRYPTION_ALGORITHM;
054            }
055    
056            protected abstract String doEncrypt(
057                            String algorithm, String plainTextPassword,
058                            String encryptedPassword)
059                    throws PwdEncryptorException;
060    
061            private static final String _PASSWORDS_ENCRYPTION_ALGORITHM =
062                    StringUtil.toUpperCase(
063                            GetterUtil.getString(
064                                    PropsUtil.get(PropsKeys.PASSWORDS_ENCRYPTION_ALGORITHM)));
065    
066    }