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.kernel.exception.PwdEncryptorException;
018    import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PropsUtil;
024    
025    /**
026     * @author Michael C. Han
027     */
028    public abstract class BasePasswordEncryptor implements PasswordEncryptor {
029    
030            @Override
031            public String encrypt(String plainTextPassword, String encryptedPassword)
032                    throws PwdEncryptorException {
033    
034                    return encrypt(
035                            getDefaultPasswordAlgorithmType(), plainTextPassword,
036                            encryptedPassword);
037            }
038    
039            @Override
040            public String encrypt(
041                            String algorithm, String plainTextPassword,
042                            String encryptedPassword)
043                    throws PwdEncryptorException {
044    
045                    if (Validator.isNull(plainTextPassword)) {
046                            throw new PwdEncryptorException("Unable to encrypt blank password");
047                    }
048    
049                    return doEncrypt(algorithm, plainTextPassword, encryptedPassword);
050            }
051    
052            @Override
053            public String getDefaultPasswordAlgorithmType() {
054                    return _PASSWORDS_ENCRYPTION_ALGORITHM;
055            }
056    
057            protected abstract String doEncrypt(
058                            String algorithm, String plainTextPassword,
059                            String encryptedPassword)
060                    throws PwdEncryptorException;
061    
062            private static final String _PASSWORDS_ENCRYPTION_ALGORITHM =
063                    StringUtil.toUpperCase(
064                            GetterUtil.getString(
065                                    PropsUtil.get(PropsKeys.PASSWORDS_ENCRYPTION_ALGORITHM)));
066    
067    }