KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > common > RandomGuid


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ThreadPermissionSession.java
20  *
21  * This object is responsible for generating a random guid. It is a copy of a
22  * class found at http://javaexchange.com/aboutRandomGUID.html. With some
23  * modifications to support Coadunation better.
24  */

25
26 // package
27
package com.rift.coad.lib.common;
28
29 // java
30
import com.rift.coad.lib.common.*;
31 import java.net.InetAddress JavaDoc;
32 import java.net.UnknownHostException JavaDoc;
33 import java.security.SecureRandom JavaDoc;
34 import java.security.MessageDigest JavaDoc;
35
36
37 /**
38  * This object is responsible for generating a random guid. It is a copy of a
39  * class found at http://javaexchange.com/aboutRandomGUID.html. With some
40  * modifications to support Coadunation better.
41  *
42  * @author Marc (copied from http://javaexchange.com/aboutRandomGUID.html)
43  */

44 public class RandomGuid {
45     
46     // singleton member variables
47
private static SecureRandom JavaDoc mySecureRand;
48     
49     // private member variables
50
private String JavaDoc s_id = null;
51     public String JavaDoc guid = "";
52     
53     
54     /*
55      * Static block to take care of one time secureRandom seed.
56      * It takes a few seconds to initialize SecureRandom. You might
57      * want to consider removing this static block or replacing
58      * it with a "time since first loaded" seed to reduce this time.
59      * This block will run only once per JVM instance.
60      */

61
62     static {
63         mySecureRand = new SecureRandom JavaDoc();
64     }
65
66
67     /*
68      * Default constructor. With no specification of security option,
69      * this constructor defaults to lower security, high performance.
70      *
71      * @exception Exception
72      */

73     private RandomGuid() throws Exception JavaDoc {
74         try {
75             s_id = InetAddress.getLocalHost().toString();
76         } catch (UnknownHostException JavaDoc ex) {
77             throw new Exception JavaDoc("Failed to retrieve the host information ["
78                     + ex.getMessage(),ex);
79         }
80         
81         // generate the random guid
82
generateRandomGUID();
83     }
84     
85     
86     /**
87      * This method returns a random guid object.
88      *
89      * @return The reference to the instance of the random guid.
90      * @exception Exception
91      */

92     public static RandomGuid getInstance() throws Exception JavaDoc {
93         return new RandomGuid();
94     }
95     
96     
97     /*
98      * This method generates the random GUID string
99      *
100      * @exception Exception
101      */

102     private void generateRandomGUID() throws Exception JavaDoc {
103         try {
104             StringBuffer JavaDoc sbValueBeforeMD5 = new StringBuffer JavaDoc();
105             // init the md5 hash
106
MessageDigest JavaDoc md5 = null;
107             md5 = MessageDigest.getInstance("MD5");
108             
109             
110             long time = new java.util.Date JavaDoc().getTime();
111             String JavaDoc rand = "";
112             
113             // generate the random value
114
synchronized (mySecureRand) {
115                 for (int count = 0; count < 20; count++)
116                 {
117                     rand += Long.toString(mySecureRand.nextLong());
118                 }
119             }
120             
121             // This StringBuffer can be a long as you need; the MD5
122
// hash will always return 128 bits. You can change
123
// the seed to include anything you want here.
124
// You could even stream a file through the MD5 making
125
// the odds of guessing it at least as great as that
126
// of guessing the contents of the file!
127
sbValueBeforeMD5.append(s_id);
128             sbValueBeforeMD5.append(":");
129             sbValueBeforeMD5.append(Long.toString(time));
130             sbValueBeforeMD5.append(":");
131             sbValueBeforeMD5.append(rand);
132             
133             // generate the md5 hash value.
134
String JavaDoc valueBeforeMD5 = sbValueBeforeMD5.toString();
135             md5.update(valueBeforeMD5.getBytes());
136             byte[] array = md5.digest();
137             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
138             for (int j = 0; j < array.length; ++j) {
139                 int b = array[j] & 0xFF;
140                 if (b < 0x10) sb.append('0');
141                 sb.append(Integer.toHexString(b));
142             }
143             
144             guid = sb.toString();
145         } catch (Exception JavaDoc ex) {
146             throw new Exception JavaDoc("Failed to generate the GUID : " +
147                     ex.getMessage(),ex);
148         }
149     }
150     
151     
152     /**
153      * This method returns the guid value.
154      *
155      * @return The string containing the guid value.
156      */

157     public String JavaDoc getGuid() {
158         return guid;
159     }
160     
161     
162     /**
163      * Retrieve the string value contained within.
164      *
165      * @return The string value.
166      */

167     public String JavaDoc toString() {
168         return guid;
169     }
170 }
171
Popular Tags