1 33 34 package com.icesoft.util; 35 36 import com.icesoft.util.encoding.Base64; 37 38 import java.net.InetAddress ; 39 import java.net.UnknownHostException ; 40 import java.security.MessageDigest ; 41 import java.security.NoSuchAlgorithmException ; 42 import java.util.Random ; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 52 public class IdGenerator { 53 private String seed; 54 private long counter; 55 private String ipAddress; 56 private static MessageDigest md5; 57 58 59 public IdGenerator() { 60 this(String.valueOf(new Random ().nextInt())); 61 } 62 63 public IdGenerator(String seed) { 64 this.seed = seed.trim(); 65 this.counter = 0; 66 try { 67 ipAddress = InetAddress.getLocalHost().getHostAddress(); 68 md5 = MessageDigest.getInstance("MD5"); 69 } catch (NoSuchAlgorithmException e) { 70 throw new RuntimeException (e); 71 } catch (UnknownHostException e) { 72 throw new RuntimeException (e); 73 } 74 } 75 76 80 public synchronized String newIdentifier() { 81 StringBuffer buffer = new StringBuffer (); 82 buffer.append(++counter); 83 buffer.append(System.currentTimeMillis()); 84 buffer.append(seed); 85 buffer.append(ipAddress); 86 buffer.append(Math.random()); 87 byte[] digest = md5.digest(buffer.toString().getBytes()); 88 byte[] encodedDigest = Base64.encodeForURL(digest); 89 return new String (encodedDigest); 90 } 91 } 92 | Popular Tags |