KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > security > SessionIDGenerator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.security;
23
24 import java.util.Random JavaDoc;
25 import java.security.MessageDigest JavaDoc;
26 import java.security.NoSuchAlgorithmException JavaDoc;
27
28 /**
29  * Generator of session id for ConnecttionToken.
30  *
31  * There should be nonguessabe and none repeting as long as the server is
32  * alive.
33  *
34  * This could by all mean be made much more secure!
35  *
36  * @author <a HREF="pra@tim.se">Peter Antman</a>
37  * @author <a HREF="hiram.chirino@jboss.org">Hiram Chirino</a>
38  * @version $Revision: 37459 $
39  */

40
41 public class SessionIDGenerator {
42    int id = 0;
43    public SessionIDGenerator() {
44       
45    }
46
47    public String JavaDoc nextSessionId() throws NoSuchAlgorithmException JavaDoc {
48       int myid = -1;
49       synchronized(this) {
50          myid=id;
51          id++;
52       }
53       String JavaDoc key = randString();
54       String JavaDoc data = randString();
55       MessageDigest JavaDoc
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 JavaDoc randString() {
64       Random JavaDoc r = new Random JavaDoc( System.currentTimeMillis());
65       return ""+r.nextLong();
66    }
67    private final String JavaDoc byteArrayToHexString(byte[] byteArray)
68     {
69         String JavaDoc res = "";
70         for (int i = 0; i < byteArray.length; i++) {
71             int x = byteArray[i];
72             if (x < 0)
73                 x += 256;
74             String JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc{
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 } // SessionIDGenerator
93
Popular Tags