KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > PasswordGenerator


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.util.Random JavaDoc;
27
28
29 /**
30  * Utility class for generating passwords.
31  *
32  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
33  */

34
35 public class PasswordGenerator
36 {
37   // --- [Constants] -----------------------------------------------------------
38

39   /**
40    * We want to have a random string with a length of 10 characters.
41    * Since we encode it base-36, we modulo the random number with this value.
42    */

43
44     private static final long MAX_RANDOM_LENGTH = 3656158440062976L; // 36 ** 10
45

46
47
48   // --- [Attributes] ----------------------------------------------------------
49

50   // Used for generating a stream of pseudorandom numbers.
51
private static final Random JavaDoc random = new Random JavaDoc();
52
53
54
55   // --- [Static] --------------------------------------------------------------
56
// --- [Constructors] --------------------------------------------------------
57

58   /**
59    * Static class; don't allow instantiation.
60    */

61   private PasswordGenerator() {}
62
63
64
65   // --- [Public] --------------------------------------------------------------
66

67   /**
68    * Generates a random 10 characters password.
69    *
70    * @return the generated password.
71    */

72   public static synchronized String JavaDoc generate()
73   {
74     return Long.toString(Math.abs(random.nextLong()) % MAX_RANDOM_LENGTH, Character.MAX_RADIX);
75   }
76
77
78
79   // --- [X implementation] ----------------------------------------------------
80
// --- [X Overrides] ---------------------------------------------------------
81
// --- [Package protected] ---------------------------------------------------
82
// --- [Private] -------------------------------------------------------------
83
// --- [Protected] -----------------------------------------------------------
84
// --- [Inner classes] -------------------------------------------------------
85
}
Popular Tags