KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > util > TicketGenerator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.util;
21
22 import com.maverick.crypto.digests.MD5Digest;
23 import com.maverick.crypto.encoders.Hex;
24 import com.maverick.crypto.security.SecureRandom;
25
26 public class TicketGenerator {
27
28   static TicketGenerator instance;
29
30   TicketGenerator() {
31   }
32
33   public static TicketGenerator getInstance() {
34     if (instance == null) {
35       instance = new TicketGenerator();
36
37     }
38     return instance;
39   }
40
41   public String JavaDoc generateUniqueTicket(String JavaDoc prefix) {
42       return generateUniqueTicket(prefix, -1);
43   }
44
45   public String JavaDoc generateUniqueTicket(String JavaDoc prefix, int len) {
46
47     MD5Digest md5 = new MD5Digest();
48     long time = System.currentTimeMillis();
49
50     md5.update( (byte) (time >> 56));
51     md5.update( (byte) (time >> 48));
52     md5.update( (byte) (time >> 40));
53     md5.update( (byte) (time >> 32));
54     md5.update( (byte) (time >> 24));
55     md5.update( (byte) (time >> 16));
56     md5.update( (byte) (time >> 8));
57     md5.update( (byte) (time >> 0));
58
59     byte[] data = new byte[256];
60     SecureRandom.getInstance().nextBytes(data);
61     md5.update(data, 0, data.length);
62
63     byte[] hash = new byte[md5.getDigestSize()];
64     md5.doFinal(hash, 0);
65
66     String JavaDoc val = prefix + new String JavaDoc(Hex.encode(hash));
67
68     if(len > 0 && len < val.length())
69         val = val.substring(0, len);
70     return val;
71   }
72
73 }
74
Popular Tags