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 @Override
036 @SuppressWarnings("deprecation")
037 public String[] getSupportedAlgorithmTypes() {
038 return new String[] {
039 PasswordEncryptorUtil.TYPE_CRYPT,
040 PasswordEncryptorUtil.TYPE_UFC_CRYPT
041 };
042 }
043
044 @Override
045 protected String doEncrypt(
046 String algorithm, String plainTextPassword,
047 String encryptedPassword)
048 throws PwdEncryptorException {
049
050 byte[] saltBytes = getSalt(encryptedPassword);
051
052 try {
053 return Crypt.crypt(
054 saltBytes, plainTextPassword.getBytes(Digester.ENCODING));
055 }
056 catch (UnsupportedEncodingException uee) {
057 throw new PwdEncryptorException(uee.getMessage(), uee);
058 }
059 }
060
061 protected byte[] getSalt(String encryptedPassword)
062 throws PwdEncryptorException {
063
064 byte[] saltBytes = null;
065
066 try {
067 if (Validator.isNull(encryptedPassword)) {
068 Random random = new Random();
069
070 int x = random.nextInt(Integer.MAX_VALUE) % _SALT.length;
071 int y = random.nextInt(Integer.MAX_VALUE) % _SALT.length;
072
073 String salt = _SALT[x].concat(_SALT[y]);
074
075 saltBytes = salt.getBytes(Digester.ENCODING);
076 }
077 else {
078 String salt = encryptedPassword.substring(0, 2);
079
080 saltBytes = salt.getBytes(Digester.ENCODING);
081 }
082 }
083 catch (UnsupportedEncodingException uee) {
084 throw new PwdEncryptorException(
085 "Unable to extract salt from encrypted password " +
086 uee.getMessage(),
087 uee);
088 }
089
090 return saltBytes;
091 }
092
093 private static final String[] _SALT = ArrayUtil.toStringArray(
094 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./".
095 toCharArray());
096
097 }