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.kernel.util.StringUtil;
023    import com.liferay.portal.util.PropsUtil;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Scott Lee
028     * @author Tomas Polesovsky
029     * @author Michael C. Han
030     */
031    public class PasswordEncryptorUtil {
032    
033            public static final String PASSWORDS_ENCRYPTION_ALGORITHM =
034                    StringUtil.toUpperCase(
035                            GetterUtil.getString(
036                                    PropsUtil.get(PropsKeys.PASSWORDS_ENCRYPTION_ALGORITHM)));
037    
038            public static final String TYPE_BCRYPT = "BCRYPT";
039    
040            /**
041             * @deprecated As of 6.1.0, replaced by {@link #TYPE_UFC_CRYPT}
042             */
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 encrypt(
081                                    PASSWORDS_ENCRYPTION_ALGORITHM, plainTextPassword,
082                                    encryptedPassword);
083                    }
084                    finally {
085                            if (_log.isDebugEnabled()) {
086                                    _log.debug(
087                                            "Password encrypted in " +
088                                                    (System.currentTimeMillis() - startTime) + "ms");
089                            }
090                    }
091            }
092    
093            public static String encrypt(
094                            String algorithm, String plainTextPassword,
095                            String encryptedPassword)
096                    throws PwdEncryptorException {
097    
098                    return _passwordEncryptor.encrypt(
099                            algorithm, plainTextPassword, encryptedPassword);
100            }
101    
102            public PasswordEncryptor getPasswordEncryptor() {
103                    return _passwordEncryptor;
104            }
105    
106            public void setPasswordEncryptor(PasswordEncryptor passwordEncryptor) {
107                    _passwordEncryptor = passwordEncryptor;
108            }
109    
110            private static Log _log = LogFactoryUtil.getLog(
111                    PasswordEncryptorUtil.class);
112    
113            private static PasswordEncryptor _passwordEncryptor;
114    
115    }