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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Amos Fong
025     */
026    public class PwdGenerator {
027    
028            public static final String KEY1 = "0123456789";
029    
030            public static final String KEY2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
031    
032            public static final String KEY3 = "abcdefghijklmnopqrstuvwxyz";
033    
034            public static String getPassword() {
035                    return getPassword(8);
036            }
037    
038            public static String getPassword(int length) {
039                    return _getPassword(KEY1 + KEY2 + KEY3, length, true);
040            }
041    
042            public static String getPassword(String key, int length) {
043                    return getPassword(key, length, true);
044            }
045    
046            public static String getPassword(
047                    String key, int length, boolean useAllKeys) {
048    
049                    return _getPassword(key, length, useAllKeys);
050            }
051    
052            public static String getPinNumber() {
053                    return _getPassword(KEY1, 4, true);
054            }
055    
056            private static String _getPassword(
057                    String key, int length, boolean useAllKeys) {
058    
059                    int keysCount = 0;
060    
061                    if (key.contains(KEY1)) {
062                            keysCount++;
063                    }
064    
065                    if (key.contains(KEY2)) {
066                            keysCount++;
067                    }
068    
069                    if (key.contains(KEY3)) {
070                            keysCount++;
071                    }
072    
073                    if (keysCount > length) {
074                            if (_log.isWarnEnabled()) {
075                                    _log.warn("Length is too short");
076                            }
077    
078                            length = keysCount;
079                    }
080    
081                    StringBuilder sb = new StringBuilder(length);
082    
083                    for (int i = 0; i < length; i++) {
084                            sb.append(key.charAt((int)(Math.random() * key.length())));
085                    }
086    
087                    String password = sb.toString();
088    
089                    if (!useAllKeys) {
090                            return password;
091                    }
092    
093                    boolean invalidPassword = false;
094    
095                    if (key.contains(KEY1)) {
096                            if (Validator.isNull(StringUtil.extractDigits(password))) {
097                                    invalidPassword = true;
098                            }
099                    }
100    
101                    if (key.contains(KEY2)) {
102                            if (password.equals(password.toLowerCase())) {
103                                    invalidPassword = true;
104                            }
105                    }
106    
107                    if (key.contains(KEY3)) {
108                            if (password.equals(password.toUpperCase())) {
109                                    invalidPassword = true;
110                            }
111                    }
112    
113                    if (invalidPassword) {
114                            return _getPassword(key, length, useAllKeys);
115                    }
116    
117                    return password;
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(PwdGenerator.class);
121    
122    }