01: /*
02: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
03: *
04: * Sun Microsystems, Inc. has intellectual property rights relating to
05: * technology embodied in the product that is described in this document.
06: * In particular, and without limitation, these intellectual property
07: * rights may include one or more of the U.S. patents listed at
08: * http://www.sun.com/patents and one or more additional patents or
09: * pending patent applications in the U.S. and in other countries.
10: *
11: * U.S. Government Rights - Commercial software. Government users are subject
12: * to the Sun Microsystems, Inc. standard license agreement and applicable
13: * provisions of the FAR and its supplements. Use is subject to license terms.
14: * This distribution may include materials developed by third parties.
15: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
16: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
17: */
18: package com.sun.portal.app.blog.password;
19:
20: /**
21: * Generate a password of a given length.
22: */
23: public class PasswordGenerator {
24: private static final char[] chars = { '0', '1', '2', '3', '4', '5',
25: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
26: 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
27: 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
28: 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
29: 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
30:
31: private int length;
32:
33: public PasswordGenerator(int length) {
34: this .length = length;
35: }
36:
37: public String get() {
38: char[] pw = new char[length];
39: int c;
40:
41: for (int i = 0; i < length; i++) {
42: int n = (int) (Math.random() * chars.length);
43: pw[i] = chars[n];
44: }
45: return new String(pw);
46: }
47: }
|