001
014
015 package com.liferay.portal.security.pwd;
016
017 import com.liferay.portal.kernel.exception.PwdEncryptorException;
018 import com.liferay.portal.kernel.security.pwd.PasswordEncryptor;
019 import com.liferay.portal.kernel.security.pwd.PasswordEncryptorUtil;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.Digester;
022 import com.liferay.portal.kernel.util.Validator;
023
024 import java.io.UnsupportedEncodingException;
025
026 import java.util.Random;
027
028 import org.vps.crypt.Crypt;
029
030
034 public class CryptPasswordEncryptor
035 extends BasePasswordEncryptor implements PasswordEncryptor {
036
037 @Override
038 public String[] getSupportedAlgorithmTypes() {
039 return new String[] {
040 PasswordEncryptorUtil.TYPE_UFC_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 Random();
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 }