1 6 7 package com.dotmarketing.util; 8 9 import java.net.InetAddress ; 10 import java.security.SecureRandom ; 11 import java.util.Random ; 12 13 122 public class GUIDGenerator { 123 124 125 public GUIDGenerator() throws Exception { 126 127 try { 128 StringBuffer stringbuffer = new StringBuffer (); 129 StringBuffer stringbuffer1 = new StringBuffer (); 130 seeder = new SecureRandom (); 131 InetAddress inetaddress = InetAddress.getLocalHost(); 132 byte abyte0[] = inetaddress.getAddress(); 133 String s = hexFormat(getInt(abyte0), 8); 134 String s1 = hexFormat(hashCode(), 8); 135 stringbuffer.append("-"); 136 stringbuffer1.append(s.substring(0, 4)); 137 stringbuffer.append(s.substring(0, 4)); 138 stringbuffer.append("-"); 139 stringbuffer1.append(s.substring(4)); 140 stringbuffer.append(s.substring(4)); 141 stringbuffer.append("-"); 142 stringbuffer1.append(s1.substring(0, 4)); 143 stringbuffer.append(s1.substring(0, 4)); 144 stringbuffer.append("-"); 145 stringbuffer1.append(s1.substring(4)); 146 stringbuffer.append(s1.substring(4)); 147 midValue = stringbuffer.toString(); 148 midValueUnformated = stringbuffer1.toString(); 149 int i = seeder.nextInt(); 150 } catch (Exception exception) { 151 throw new Exception ("error - failure to instantiate GUIDGenerator" + exception); 152 } 153 } 154 155 169 private String getVal(String s) { 170 int i = (int) System.currentTimeMillis() & 0xffffffff; 171 int j = seeder.nextInt(); 172 return hexFormat(i, 8) + s + hexFormat(j, 8); 173 } 174 175 192 public String getUnformatedUUID() { 193 return getVal(midValueUnformated); 194 } 195 196 205 public String getUUID() { 206 return getVal(midValue); 207 } 208 209 221 private int getInt(byte abyte0[]) { 222 int i = 0; 223 int j = 24; 224 for (int k = 0; j >= 0; k++) { 225 int l = abyte0[k] & 0xff; 226 i += l << j; 227 j -= 8; 228 } 229 230 return i; 231 } 232 233 249 private String hexFormat(int i, int j) { 250 String s = Integer.toHexString(i); 251 return padHex(s, j) + s; 252 } 253 254 271 private String padHex(String s, int i) { 272 StringBuffer stringbuffer = new StringBuffer (); 273 if (s.length() < i) { 274 for (int j = 0; j < i - s.length(); j++) 275 stringbuffer.append("0"); 276 277 } 278 return stringbuffer.toString(); 279 } 280 281 290 private SecureRandom seeder; 291 292 301 private String midValue; 302 303 313 private String midValueUnformated; 314 } 315
| Popular Tags
|