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.kernel.security.pwd;
016    
017    import com.liferay.portal.kernel.exception.PwdEncryptorException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Scott Lee
025     * @author Tomas Polesovsky
026     * @author Michael C. Han
027     */
028    public class PasswordEncryptorUtil {
029    
030            /**
031             * @deprecated As of 7.0.0, replaced by {@link
032             *             #getDefaultPasswordAlgorithmType}
033             */
034            @Deprecated
035            public static String PASSWORDS_ENCRYPTION_ALGORITHM = StringPool.BLANK;
036    
037            public static final String TYPE_BCRYPT = "BCRYPT";
038    
039            public static final String TYPE_MD2 = "MD2";
040    
041            public static final String TYPE_MD5 = "MD5";
042    
043            public static final String TYPE_NONE = "NONE";
044    
045            public static final String TYPE_PBKDF2 = "PBKDF2";
046    
047            public static final String TYPE_SHA = "SHA";
048    
049            public static final String TYPE_SHA_256 = "SHA-256";
050    
051            public static final String TYPE_SHA_384 = "SHA-384";
052    
053            public static final String TYPE_SSHA = "SSHA";
054    
055            public static final String TYPE_UFC_CRYPT = "UFC-CRYPT";
056    
057            public static String encrypt(String plainTextPassword)
058                    throws PwdEncryptorException {
059    
060                    return encrypt(plainTextPassword, null);
061            }
062    
063            public static String encrypt(
064                            String plainTextPassword, String encryptedPassword)
065                    throws PwdEncryptorException {
066    
067                    long startTime = 0;
068    
069                    if (_log.isDebugEnabled()) {
070                            startTime = System.currentTimeMillis();
071                    }
072    
073                    try {
074                            return _passwordEncryptor.encrypt(
075                                    plainTextPassword, encryptedPassword);
076                    }
077                    finally {
078                            if (_log.isDebugEnabled()) {
079                                    _log.debug(
080                                            "Password encrypted in " +
081                                                    (System.currentTimeMillis() - startTime) + "ms");
082                            }
083                    }
084            }
085    
086            public static String encrypt(
087                            String algorithm, String plainTextPassword,
088                            String encryptedPassword)
089                    throws PwdEncryptorException {
090    
091                    return _passwordEncryptor.encrypt(
092                            algorithm, plainTextPassword, encryptedPassword);
093            }
094    
095            public static String getDefaultPasswordAlgorithmType() {
096                    return _passwordEncryptor.getDefaultPasswordAlgorithmType();
097            }
098    
099            public PasswordEncryptor getPasswordEncryptor() {
100                    return _passwordEncryptor;
101            }
102    
103            public void setPasswordEncryptor(PasswordEncryptor passwordEncryptor) {
104                    _passwordEncryptor = passwordEncryptor;
105    
106                    PASSWORDS_ENCRYPTION_ALGORITHM =
107                            _passwordEncryptor.getDefaultPasswordAlgorithmType();
108            }
109    
110            private static final Log _log = LogFactoryUtil.getLog(
111                    PasswordEncryptorUtil.class);
112    
113            private static PasswordEncryptor _passwordEncryptor;
114    
115    }