1 22 package org.jboss.mq.security; 23 24 import java.util.Random ; 25 import java.security.MessageDigest ; 26 import java.security.NoSuchAlgorithmException ; 27 28 40 41 public class SessionIDGenerator { 42 int id = 0; 43 public SessionIDGenerator() { 44 45 } 46 47 public String nextSessionId() throws NoSuchAlgorithmException { 48 int myid = -1; 49 synchronized(this) { 50 myid=id; 51 id++; 52 } 53 String key = randString(); 54 String data = randString(); 55 MessageDigest 56 md5=java.security.MessageDigest.getInstance("MD5"); 57 md5.update(String.valueOf(myid).getBytes()); 58 md5.update(data.getBytes()); 59 md5.update(data.getBytes()); 60 byte[] byteHash = md5.digest(key.getBytes()); 61 return byteArrayToHexString(byteHash); 62 } 63 String randString() { 64 Random r = new Random ( System.currentTimeMillis()); 65 return ""+r.nextLong(); 66 } 67 private final String byteArrayToHexString(byte[] byteArray) 68 { 69 String res = ""; 70 for (int i = 0; i < byteArray.length; i++) { 71 int x = byteArray[i]; 72 if (x < 0) 73 x += 256; 74 String xs = Integer.toHexString(x); 75 while (xs.length() < 2) 76 xs = "0" + xs; 77 res += xs; 78 } 79 return res; 80 } 81 public static void main(String [] args) throws Exception { 82 int rounds = 1000; 83 if (args.length == 1) 84 rounds = Integer.parseInt(args[0]); 85 86 SessionIDGenerator gen = new SessionIDGenerator(); 87 for (int i =0;i<rounds;i++) { 88 System.out.println(gen.nextSessionId()); 89 } 90 } 91 92 } | Popular Tags |