package com.liferay.util; import com.liferay.portal.kernel.util.StringMaker; /** * <a href="PwdGenerator.java.html"><b><i>View Source</i></b></a> * * @author Brian Wing Shun Chan * */ public class PwdGenerator { public static String KEY1 = "0123456789"; public static String KEY2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static String KEY3 = "abcdefghijklmnopqrstuvwxyz"; public static String getPinNumber() { return _getPassword(KEY1, 4); } public static String getPassword() { return getPassword(8); } public static String getPassword(int length) { return _getPassword(KEY1 + KEY2 + KEY3, length); } public static String getPassword(String key, int length) { return _getPassword(key, length); } private static String _getPassword(String key, int length) { StringMaker sm = new StringMaker(); for (int i = 0; i < length; i++) { sm.append(key.charAt((int)(Math.random() * key.length()))); } return sm.toString(); } }
package com.liferay.portal.security.pwd; import com.liferay.portal.PwdEncryptorException; import com.liferay.portal.kernel.util.Base64; import com.liferay.portal.kernel.util.Digester; import com.liferay.portal.kernel.util.DigesterUtil; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.StringMaker; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.util.PropsUtil; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Random; import org.vps.crypt.Crypt; /** * <a href="PwdEncryptor.java.html"><b><i>View Source</i></b></a> * * @author Brian Wing Shun Chan * @author Scott Lee * */ public class PwdEncryptor { public static final String PASSWORDS_ENCRYPTION_ALGORITHM = GetterUtil.getString(PropsUtil.get( PropsUtil.PASSWORDS_ENCRYPTION_ALGORITHM)).toUpperCase(); public static final String TYPE_CRYPT = "CRYPT"; public static final String TYPE_MD2 = "MD2"; public static final String TYPE_MD5 = "MD5"; public static final String TYPE_NONE = "NONE"; public static final String TYPE_SHA = "SHA"; public static final String TYPE_SHA_256 = "SHA-256"; public static final String TYPE_SHA_384 = "SHA-384"; public static final String TYPE_SSHA = "SSHA"; public static final char[] saltChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./" .toCharArray(); public static String encrypt(String clearTextPwd) throws PwdEncryptorException { return encrypt(PASSWORDS_ENCRYPTION_ALGORITHM, clearTextPwd, null); } public static String encrypt(String clearTextPwd, String currentEncPwd) throws PwdEncryptorException { return encrypt( PASSWORDS_ENCRYPTION_ALGORITHM, clearTextPwd, currentEncPwd); } public static String encrypt( String algorithm, String clearTextPwd, String currentEncPwd) throws PwdEncryptorException { if (algorithm.equals(TYPE_CRYPT)) { byte[] saltBytes = _getSaltFromCrypt(currentEncPwd); return encodePassword(algorithm, clearTextPwd, saltBytes); } else if (algorithm.equals(TYPE_NONE)) { return clearTextPwd; } else if (algorithm.equals(TYPE_SSHA)) { byte[] saltBytes = _getSaltFromSSHA(currentEncPwd); return encodePassword(algorithm, clearTextPwd, saltBytes); } else { return encodePassword(algorithm, clearTextPwd, null); } } protected static String encodePassword( String algorithm, String clearTextPwd, byte[] saltBytes) throws PwdEncryptorException { try { if (algorithm.equals(TYPE_CRYPT)) { return Crypt.crypt( clearTextPwd.getBytes(Digester.ENCODING), saltBytes); } else if (algorithm.equals(TYPE_SSHA)) { byte[] clearTextPwdBytes = clearTextPwd.getBytes(Digester.ENCODING); // Create a byte array of salt bytes appeneded to password bytes byte[] pwdPlusSalt = new byte[clearTextPwdBytes.length + saltBytes.length]; System.arraycopy( clearTextPwdBytes, 0, pwdPlusSalt, 0, clearTextPwdBytes.length); System.arraycopy( saltBytes, 0, pwdPlusSalt, clearTextPwdBytes.length, saltBytes.length); // Digest byte array MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1"); byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt); // Appends salt bytes to the SHA-1 digest. byte[] digestPlusSalt = new byte[pwdPlusSaltHash.length + saltBytes.length]; System.arraycopy( pwdPlusSaltHash, 0, digestPlusSalt, 0, pwdPlusSaltHash.length); System.arraycopy( saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length, saltBytes.length); // Base64 encode and format string return Base64.encode(digestPlusSalt); } else { return DigesterUtil.digest(algorithm, clearTextPwd); } } catch (NoSuchAlgorithmException nsae) { throw new PwdEncryptorException(nsae.getMessage()); } catch (UnsupportedEncodingException uee) { throw new PwdEncryptorException(uee.getMessage()); } } private static byte[] _getSaltFromCrypt(String cryptString) throws PwdEncryptorException { byte[] saltBytes = new byte[2]; try { if (Validator.isNull(cryptString)) { // Generate random salt Random randomGenerator = new Random(); int numSaltChars = saltChars.length; StringMaker sm = new StringMaker(); int x = Math.abs(randomGenerator.nextInt()) % numSaltChars; int y = Math.abs(randomGenerator.nextInt()) % numSaltChars; sm.append(saltChars[x]); sm.append(saltChars[y]); String salt = sm.toString(); saltBytes = salt.getBytes(Digester.ENCODING); } else { // Extract salt from encrypted password String salt = cryptString.substring(0, 3); saltBytes = salt.getBytes(Digester.ENCODING); } } catch (UnsupportedEncodingException uee) { throw new PwdEncryptorException( "Unable to extract salt from encrypted password: " + uee.getMessage()); } return saltBytes; } private static byte[] _getSaltFromSSHA(String sshaString) throws PwdEncryptorException { byte[] saltBytes = new byte[8]; if (Validator.isNull(sshaString)) { // Generate random salt Random random = new SecureRandom(); random.nextBytes(saltBytes); } else { // Extract salt from encrypted password try { byte[] digestPlusSalt = Base64.decode(sshaString); byte[] digestBytes = new byte[digestPlusSalt.length - 8]; System.arraycopy( digestPlusSalt, 0, digestBytes, 0, digestBytes.length); System.arraycopy( digestPlusSalt, digestBytes.length, saltBytes, 0, saltBytes.length); } catch (Exception e) { throw new PwdEncryptorException( "Unable to extract salt from encrypted password: " + e.getMessage()); } } return saltBytes; } }
package com.liferay.portal.util; import com.liferay.portal.kernel.util.Base64; import com.liferay.portal.kernel.util.Digester; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * <a href="DigesterImpl.java.html"><b><i>View Source</i></b></a> * * @author Brian Wing Shun Chan * */ public class DigesterImpl implements Digester { public String digest(String text) { return digest(Digester.DIGEST_ALGORITHM, text); } public String digest(String algorithm, String text) { MessageDigest digester = null; try{ digester = MessageDigest.getInstance(algorithm); digester.update(text.getBytes(Digester.ENCODING)); } catch (NoSuchAlgorithmException nsae) { _log.error(nsae, nsae); } catch (UnsupportedEncodingException uee) { _log.error(uee, uee); } byte[] byteArray = digester.digest(); if (_BASE_64) { return Base64.encode(byteArray); } else { return new String(Hex.encodeHex(byteArray)); } } private static final boolean _BASE_64 = PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64"); private static Log _log = LogFactory.getLog(Digester.class); }
package com.liferay.portal.kernel.util; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * <a href="Base64.java.html"><b><i>View Source</i></b></a> * * @author Brian Wing Shun Chan * */ public class Base64 { protected static char getChar(int sixbit) { if (sixbit >= 0 && sixbit <= 25) { return (char)(65 + sixbit); } if (sixbit >= 26 && sixbit <= 51) { return (char)(97 + (sixbit - 26)); } if (sixbit >= 52 && sixbit <= 61) { return (char)(48 + (sixbit - 52)); } if (sixbit == 62) { return '+'; } return sixbit != 63 ? '?' : '/'; } protected static int getValue(char c) { if (c >= 'A' && c <= 'Z') { return c - 65; } if (c >= 'a' && c <= 'z') { return (c - 97) + 26; } if (c >= '0' && c <= '9') { return (c - 48) + 52; } if (c == '+') { return 62; } if (c == '/') { return 63; } return c != '=' ? -1 : 0; } public static String encode(byte raw[]) { StringMaker encoded = new StringMaker(); for (int i = 0; i < raw.length; i += 3) { encoded.append(encodeBlock(raw, i)); } return encoded.toString(); } protected static char[] encodeBlock(byte raw[], int offset) { int block = 0; int slack = raw.length - offset - 1; int end = slack < 2 ? slack : 2; for (int i = 0; i <= end; i++) { byte b = raw[offset + i]; int neuter = b >= 0 ? ((int) (b)) : b + 256; block += neuter << 8 * (2 - i); } char base64[] = new char[4]; for (int i = 0; i < 4; i++) { int sixbit = block >>> 6 * (3 - i) & 0x3f; base64[i] = getChar(sixbit); } if (slack < 1) { base64[2] = '='; } if (slack < 2) { base64[3] = '='; } return base64; } public static byte[] decode(String base64) { int pad = 0; for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) { pad++; } int length = (base64.length() * 6) / 8 - pad; byte raw[] = new byte[length]; int rawindex = 0; for (int i = 0; i < base64.length(); i += 4) { int block = (getValue(base64.charAt(i)) << 18) + (getValue(base64.charAt(i + 1)) << 12) + (getValue(base64.charAt(i + 2)) << 6) + getValue(base64.charAt(i + 3)); for (int j = 0; j < 3 && rawindex + j < raw.length; j++) { raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff); } rawindex += 3; } return raw; } public static String objectToString(Object o) { if (o == null) { return null; } ByteArrayMaker bam = new ByteArrayMaker(32000); try { ObjectOutputStream os = new ObjectOutputStream( new BufferedOutputStream(bam)); os.flush(); os.writeObject(o); os.flush(); } catch (IOException e) { _log.error(e.getMessage()); } return encode(bam.toByteArray()); } public static Object stringToObject(String s) { if (s == null) { return null; } byte byteArray[] = decode(s); ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); try { ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(bais)); return is.readObject(); } catch (Exception e) { _log.error(e.getMessage()); } return null; } private static Log _log = LogFactoryUtil.getLog(Base64.class); }