| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.util; |
| 20 | |
| 21 | import java.security.SecureRandom; |
| 22 | |
| 23 | import com.google.common.base.Supplier; |
| 24 | |
| 25 | /** |
| 26 | * Cheap, lightweight, low-security password generator. |
| 27 | * |
| 28 | * @see <a href= |
| 29 | * "http://www.java-forums.org/java-lang/7355-how-create-lightweight-low-security-password-generator.html" |
| 30 | * /> |
| 31 | */ |
| 32 | public enum PasswordGenerator implements Supplier<String> { |
| 33 | |
| 34 | INSTANCE; |
| 35 | |
| 36 | /** Minimum length for a decent password */ |
| 37 | public static final int MIN_LENGTH = 10; |
| 38 | |
| 39 | /** The random number generator. */ |
| 40 | protected static final SecureRandom r = new SecureRandom(); |
| 41 | |
| 42 | /* |
| 43 | * Set of characters that is valid. Must be printable, memorable, and |
| 44 | * "won't break HTML" (i.e., not ' <', '>', '&', '=', ...). or break shell |
| 45 | * commands (i.e., not ' <', '>', '$', '!', ...). I, L and O are good to |
| 46 | * leave out, as are numeric zero and one. |
| 47 | */ |
| 48 | public final static char[] goodChar = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', |
| 49 | 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', |
| 50 | 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '@', }; |
| 51 | |
| 52 | @Override |
| 53 | public String get() { |
| 54 | StringBuffer sb = new StringBuffer(); |
| 55 | for (int i = 0; i < MIN_LENGTH; i++) { |
| 56 | sb.append(goodChar[r.nextInt(goodChar.length)]); |
| 57 | } |
| 58 | return sb.toString(); |
| 59 | } |
| 60 | } |