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.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.Digester;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.UnsupportedEncodingException;
023    
024    import java.util.Random;
025    
026    import org.vps.crypt.Crypt;
027    
028    /**
029     * @author Michael C. Han
030     * @author Tomas Polesovsky
031     */
032    public class CryptPasswordEncryptor
033            extends BasePasswordEncryptor implements PasswordEncryptor {
034    
035            @SuppressWarnings("deprecation")
036            public String[] getSupportedAlgorithmTypes() {
037                    return new String[] {
038                            PasswordEncryptorUtil.TYPE_CRYPT,
039                            PasswordEncryptorUtil.TYPE_UFC_CRYPT
040                    };
041            }
042    
043            @Override
044            protected String doEncrypt(
045                            String algorithm, String plainTextPassword,
046                            String encryptedPassword)
047                    throws PwdEncryptorException {
048    
049                    byte[] saltBytes = getSalt(encryptedPassword);
050    
051                    try {
052                            return Crypt.crypt(
053                                    saltBytes, plainTextPassword.getBytes(Digester.ENCODING));
054                    }
055                    catch (UnsupportedEncodingException uee) {
056                            throw new PwdEncryptorException(uee.getMessage(), uee);
057                    }
058            }
059    
060            protected byte[] getSalt(String encryptedPassword)
061                    throws PwdEncryptorException {
062    
063                    byte[] saltBytes = null;
064    
065                    try {
066                            if (Validator.isNull(encryptedPassword)) {
067                                    Random random = new Random();
068    
069                                    int x = random.nextInt(Integer.MAX_VALUE) % _SALT.length;
070                                    int y = random.nextInt(Integer.MAX_VALUE) % _SALT.length;
071    
072                                    String salt = _SALT[x].concat(_SALT[y]);
073    
074                                    saltBytes = salt.getBytes(Digester.ENCODING);
075                            }
076                            else {
077                                    String salt = encryptedPassword.substring(0, 2);
078    
079                                    saltBytes = salt.getBytes(Digester.ENCODING);
080                            }
081                    }
082                    catch (UnsupportedEncodingException uee) {
083                            throw new PwdEncryptorException(
084                                    "Unable to extract salt from encrypted password " +
085                                            uee.getMessage(),
086                                    uee);
087                    }
088    
089                    return saltBytes;
090            }
091    
092            private static final String[] _SALT = ArrayUtil.toStringArray(
093                    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
094                            .toCharArray());
095    
096    }