Java Password Generate generatePassword()

Here you can find the source of generatePassword()

Description

Generate password from alpha numerics characters in total length 8 characters.

License

Apache License

Declaration

public static String generatePassword() 

Method Source Code


//package com.java2s;
/*/*from   w  w w .j  a va2s  . co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Random;

public class Main {
    private static final int PASSWORD_LENGT = 8;

    /**
     * Generate password from alpha numerics characters in total length 8 characters. 
     * @return
     */
    public static String generatePassword() {
        final char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
        final StringBuilder sb = new StringBuilder();
        final Random random = new Random();

        for (int i = 0; i < PASSWORD_LENGT; i++) {
            final char c = chars[random.nextInt(chars.length)];
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. generatePassword()
  2. generatePassword()
  3. generatePassword()
  4. generatePassword()
  5. generatePassword()
  6. generatePassword(int lenght)
  7. generatePassword(int length)