001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.security;
029
030import java.util.Random;
031
032import org.apache.commons.lang3.ArrayUtils;
033
034/**
035 * Default Password Generator class.<p>
036 */
037public class CmsDefaultPasswordGenerator implements I_CmsPasswordGenerator {
038
039    /**Capital letters. */
040    private static final String[] Capitals = {
041        "A",
042        "B",
043        "C",
044        "D",
045        "E",
046        "F",
047        "G",
048        "H",
049        "I",
050        "J",
051        "K",
052        "L",
053        "M",
054        "N",
055        "O",
056        "P",
057        "Q",
058        "R",
059        "S",
060        "T",
061        "U",
062        "V",
063        "W",
064        "X",
065        "Y",
066        "Z"};
067
068    /**Letters. */
069    private static final String[] Letters = {
070        "a",
071        "b",
072        "c",
073        "d",
074        "e",
075        "f",
076        "g",
077        "h",
078        "i",
079        "j",
080        "k",
081        "l",
082        "m",
083        "n",
084        "o",
085        "p",
086        "q",
087        "r",
088        "s",
089        "t",
090        "u",
091        "v",
092        "w",
093        "x",
094        "y",
095        "z"};
096
097    /**Numbers. */
098    private static final String[] Numbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
099
100    /**Special chars. */
101    private static final String[] Specials = {
102        "!",
103        "?",
104        "=",
105        "*",
106        "+",
107        "-",
108        "#",
109        "$",
110        "%",
111        "&",
112        ":",
113        "(",
114        ")",
115        "[",
116        "]"};
117
118    /**
119     * Get a random password.<p>
120     *
121     * @return random password
122     */
123    public static String getRandomPWD() {
124
125        return getRandomPWD(10, 2, 2);
126    }
127
128    /**
129     * Get a random password.<p>
130     * @param countTotal Desired password length
131     * @param countCapitals minimal count of Capital letters
132     * @param countSpecials count of special chars
133     *
134     * @return random password
135     */
136    public static String getRandomPWD(int countTotal, int countCapitals, int countSpecials) {
137
138        CmsDefaultPasswordGenerator generator = new CmsDefaultPasswordGenerator();
139        return generator.getRandomPassword(countTotal, countCapitals, countSpecials);
140    }
141
142    /**
143     * @see org.opencms.security.I_CmsPasswordGenerator#getRandomPassword()
144     */
145    public String getRandomPassword() {
146
147        return getRandomPassword(10, 2, 2);
148    }
149
150    /**
151     * Generates a random password.<p>
152     *
153     * @param countTotal Total password length
154     * @param countCapitals Minimal count of capitals
155     * @param countSpecials count of special chars
156     * @return random password
157     */
158    public String getRandomPassword(int countTotal, int countCapitals, int countSpecials) {
159
160        String res = "";
161
162        String[] Normals = ArrayUtils.addAll(ArrayUtils.addAll(Capitals, Letters), Numbers);
163        Random rand = new Random();
164        for (int i = 0; i < (countTotal - countCapitals - countSpecials); i++) {
165            res = res + Normals[rand.nextInt(Normals.length)];
166        }
167        for (int i = 0; i < countSpecials; i++) {
168            int pos = rand.nextInt(res.length());
169            res = res.substring(0, pos) + Specials[rand.nextInt(Specials.length)] + res.substring(pos);
170        }
171        for (int i = 0; i < countCapitals; i++) {
172            int pos = rand.nextInt(res.length());
173            res = res.substring(0, pos) + Capitals[rand.nextInt(Capitals.length)] + res.substring(pos);
174        }
175        return res;
176    }
177
178}