001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.pwd;
016    
017    import com.liferay.portal.PwdEncryptorException;
018    import com.liferay.portal.kernel.security.SecureRandom;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.Digester;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.UnsupportedEncodingException;
024    
025    import java.util.Random;
026    
027    import org.vps.crypt.Crypt;
028    
029    /**
030     * @author Michael C. Han
031     * @author Tomas Polesovsky
032     */
033    public class CryptPasswordEncryptor
034            extends BasePasswordEncryptor implements PasswordEncryptor {
035    
036            @Override
037            @SuppressWarnings("deprecation")
038            public String[] getSupportedAlgorithmTypes() {
039                    return new String[] {
040                            PasswordEncryptorUtil.TYPE_CRYPT,
041                            PasswordEncryptorUtil.TYPE_UFC_CRYPT
042                    };
043            }
044    
045            @Override
046            protected String doEncrypt(
047                            String algorithm, String plainTextPassword,
048                            String encryptedPassword)
049                    throws PwdEncryptorException {
050    
051                    byte[] saltBytes = getSalt(encryptedPassword);
052    
053                    try {
054                            return Crypt.crypt(
055                                    saltBytes, plainTextPassword.getBytes(Digester.ENCODING));
056                    }
057                    catch (UnsupportedEncodingException uee) {
058                            throw new PwdEncryptorException(uee.getMessage(), uee);
059                    }
060            }
061    
062            protected byte[] getSalt(String encryptedPassword)
063                    throws PwdEncryptorException {
064    
065                    byte[] saltBytes = null;
066    
067                    try {
068                            if (Validator.isNull(encryptedPassword)) {
069                                    Random random = new SecureRandom();
070    
071                                    int x = random.nextInt(Integer.MAX_VALUE) % _SALT.length;
072                                    int y = random.nextInt(Integer.MAX_VALUE) % _SALT.length;
073    
074                                    String salt = _SALT[x].concat(_SALT[y]);
075    
076                                    saltBytes = salt.getBytes(Digester.ENCODING);
077                            }
078                            else {
079                                    String salt = encryptedPassword.substring(0, 2);
080    
081                                    saltBytes = salt.getBytes(Digester.ENCODING);
082                            }
083                    }
084                    catch (UnsupportedEncodingException uee) {
085                            throw new PwdEncryptorException(
086                                    "Unable to extract salt from encrypted password " +
087                                            uee.getMessage(),
088                                    uee);
089                    }
090    
091                    return saltBytes;
092            }
093    
094            private static final String[] _SALT = ArrayUtil.toStringArray(
095                    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./".
096                            toCharArray());
097    
098    }