Java Password Generate generatePassword()

Here you can find the source of generatePassword()

Description

Generates a random password of 8 characters

License

Open Source License

Return

The randomized password as a String

Declaration

public static String generatePassword() 

Method Source Code

//package com.java2s;
/*//from w  ww  .jav a  2 s .  c om
 *   Description: Application used for managing a chemical storage solution.
 *                This application handles users, compounds, containers,
 *                suppliers, locations, labelprinting and everything else
 *                neded to manage a chemical storage, based on the java technology.
 *                In addition it includes a sample module. This module, is used
 *               to create samples, store results etc.
 *
 *   Copyright:   Copyright Dann Vestergaard and Claus Stie Kallesoe 2003-2006.
 *              All rights reserved.
 *
 *   overLIB:     overLIB 3.51  -- Copyright Erik Bosrup 1998-2002. All rights reserved.
 *
 *   This file is part of chemicalinventory.
 *
 *   chemicalinventory 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
 *   any later version.
 *
 *   chemicalinventory 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.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with chemicalinventory; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

import java.util.Random;

public class Main {
    /**
     * Generates a random password of 8 characters
     * @return The randomized password as a String
     */
    public static String generatePassword() {
        String password = "";
        String values[] = { "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s",
                "t", "u", "v", "z", "x", "y", "1", "2", "3", "4", "5", "6", "7", "8", "9", "&", "%", "?", "!", "A",
                "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "Z",
                "X", "Y" };

        Random ran = new Random();
        int range = values.length;
        int number;

        while (password.length() != 8) {
            number = ran.nextInt(range);
            password = password + values[number];
        }
        System.out.println("password random value: " + password);

        return password;
    }
}

Related

  1. generatePassword()
  2. generatePassword()
  3. generatePassword()
  4. generatePassword()