1 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 generateUniqueTicket(String prefix) { 42 return generateUniqueTicket(prefix, -1); 43 } 44 45 public String generateUniqueTicket(String 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 val = prefix + new String (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 |