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.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            /**
040             * @deprecated As of 6.1.0, replaced by {@link #TYPE_UFC_CRYPT}
041             */
042            @Deprecated
043            public static final String TYPE_CRYPT = "CRYPT";
044    
045            public static final String TYPE_MD2 = "MD2";
046    
047            public static final String TYPE_MD5 = "MD5";
048    
049            public static final String TYPE_NONE = "NONE";
050    
051            public static final String TYPE_PBKDF2 = "PBKDF2";
052    
053            public static final String TYPE_SHA = "SHA";
054    
055            public static final String TYPE_SHA_256 = "SHA-256";
056    
057            public static final String TYPE_SHA_384 = "SHA-384";
058    
059            public static final String TYPE_SSHA = "SSHA";
060    
061            public static final String TYPE_UFC_CRYPT = "UFC-CRYPT";
062    
063            public static String encrypt(String plainTextPassword)
064                    throws PwdEncryptorException {
065    
066                    return encrypt(plainTextPassword, null);
067            }
068    
069            public static String encrypt(
070                            String plainTextPassword, String encryptedPassword)
071                    throws PwdEncryptorException {
072    
073                    long startTime = 0;
074    
075                    if (_log.isDebugEnabled()) {
076                            startTime = System.currentTimeMillis();
077                    }
078    
079                    try {
080                            return _passwordEncryptor.encrypt(
081                                    plainTextPassword, encryptedPassword);
082                    }
083                    finally {
084                            if (_log.isDebugEnabled()) {
085                                    _log.debug(
086                                            "Password encrypted in " +
087                                                    (System.currentTimeMillis() - startTime) + "ms");
088                            }
089                    }
090            }
091    
092            public static String encrypt(
093                            String algorithm, String plainTextPassword,
094                            String encryptedPassword)
095                    throws PwdEncryptorException {
096    
097                    return _passwordEncryptor.encrypt(
098                            algorithm, plainTextPassword, encryptedPassword);
099            }
100    
101            public static String getDefaultPasswordAlgorithmType() {
102                    return _passwordEncryptor.getDefaultPasswordAlgorithmType();
103            }
104    
105            public PasswordEncryptor getPasswordEncryptor() {
106                    return _passwordEncryptor;
107            }
108    
109            public void setPasswordEncryptor(PasswordEncryptor passwordEncryptor) {
110                    _passwordEncryptor = passwordEncryptor;
111    
112                    PASSWORDS_ENCRYPTION_ALGORITHM =
113                            _passwordEncryptor.getDefaultPasswordAlgorithmType();
114            }
115    
116            private static final Log _log = LogFactoryUtil.getLog(
117                    PasswordEncryptorUtil.class);
118    
119            private static PasswordEncryptor _passwordEncryptor;
120    
121    }