001
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
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 }