no.abmu.user.util.UserUtil.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.user.util.UserUtil.java

Source

/*$Id: UserUtil.java 9244 2008-03-21 23:55:22Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2005 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */

package no.abmu.user.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Random;

/**
 * UserUtil.
 * 
 * @author Erik Romson, erik@zenior.no
 * @author $Author: jens $
 * @version $Rev: 9244 $
 * @date $Date: 2008-03-22 00:55:22 +0100 (Sat, 22 Mar 2008) $
 * @copyright ABM-Utvikling
 */
public final class UserUtil {

    private static final Log logger = (Log) LogFactory.getLog(UserUtil.class);

    private UserUtil() {
    }

    public static String generatePassword() {
        if (true) {
            String.valueOf(System.currentTimeMillis());
        }

        StringBuffer passwd = new StringBuffer(10);
        try {
            char[] numChars = { '2', '3', '4', '5', '6', '7', '8', '9' };
            char[] smallChars = { 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't',
                    'u', 'v', 'w', 'x', 'y', 'z' };
            char[] capChars = { 'A', 'B', 'C', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
                    'U', 'V', 'W', 'X', 'Y', 'Z' };
            Random random = new Random((long) ((double) System.currentTimeMillis() * Math.random()));
            for (int i = 0; i <= 2; i++) {
                int j = random.nextInt(smallChars.length + 4);
                if (j < smallChars.length && j >= 0) {
                    passwd.append(smallChars[j]);
                }
                j = random.nextInt(capChars.length + 4);
                if (j < capChars.length && j >= 0) {
                    passwd.append(capChars[j]);
                }
                j = random.nextInt(numChars.length + 4);
                if (j < numChars.length && j >= 0) {
                    passwd.append(numChars[j]);
                }
            }
        } catch (Exception e) {
            logger.error("[generatePassword]: had some problems ! " + e.toString());
        }
        if (passwd.length() > 8) {
            return passwd.substring(0, 7);
        }
        return passwd.toString();
    }

    public static void main(String[] args) {
        System.out.println(generatePassword());
    }

}