001    /**
002     * Copyright (c) 2000-2013 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.PwdEncryptorException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.util.PropsUtil;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Scott Lee
027     * @author Tomas Polesovsky
028     * @author Michael C. Han
029     */
030    public class PasswordEncryptorUtil {
031    
032            public static final String PASSWORDS_ENCRYPTION_ALGORITHM =
033                    GetterUtil.getString(
034                            PropsUtil.get(
035                                    PropsKeys.PASSWORDS_ENCRYPTION_ALGORITHM)).toUpperCase();
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            public static final String TYPE_CRYPT = "CRYPT";
043    
044            public static final String TYPE_MD2 = "MD2";
045    
046            public static final String TYPE_MD5 = "MD5";
047    
048            public static final String TYPE_NONE = "NONE";
049    
050            public static final String TYPE_PBKDF2 = "PBKDF2";
051    
052            public static final String TYPE_SHA = "SHA";
053    
054            public static final String TYPE_SHA_256 = "SHA-256";
055    
056            public static final String TYPE_SHA_384 = "SHA-384";
057    
058            public static final String TYPE_SSHA = "SSHA";
059    
060            public static final String TYPE_UFC_CRYPT = "UFC-CRYPT";
061    
062            public static String encrypt(String plainTextPassword)
063                    throws PwdEncryptorException {
064    
065                    return encrypt(plainTextPassword, null);
066            }
067    
068            public static String encrypt(
069                            String plainTextPassword, String encryptedPassword)
070                    throws PwdEncryptorException {
071    
072                    long startTime = 0;
073    
074                    if (_log.isDebugEnabled()) {
075                            startTime = System.currentTimeMillis();
076                    }
077    
078                    try {
079                            return encrypt(
080                                    PASSWORDS_ENCRYPTION_ALGORITHM, plainTextPassword,
081                                    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 PasswordEncryptor getPasswordEncryptor() {
102                    return _passwordEncryptor;
103            }
104    
105            public void setPasswordEncryptor(PasswordEncryptor passwordEncryptor) {
106                    _passwordEncryptor = passwordEncryptor;
107            }
108    
109            private static Log _log = LogFactoryUtil.getLog(
110                    PasswordEncryptorUtil.class);
111    
112            private static PasswordEncryptor _passwordEncryptor;
113    
114    }