1 55 package org.jboss.axis.utils; 56 57 import org.jboss.logging.Logger; 58 59 import java.security.MessageDigest ; 60 import java.security.NoSuchAlgorithmException ; 61 import java.util.Random ; 62 63 66 public class SessionUtils 67 { 68 69 72 private static Logger log = Logger.getLogger(SessionUtils.class.getName()); 73 74 78 protected static final String DEFAULT_ALGORITHM = "MD5"; 79 80 84 protected static final int SESSION_ID_BYTES = 16; 85 86 91 protected static String algorithm = DEFAULT_ALGORITHM; 92 93 97 protected static MessageDigest digest = null; 98 99 102 protected static Random random = null; 103 104 108 protected static String randomClass = "java.security.SecureRandom"; 109 110 113 private static String thisHost = null; 114 115 120 public static synchronized String generateSessionId() 121 { 122 byte bytes[] = new byte[SESSION_ID_BYTES]; 124 125 getRandom().nextBytes(bytes); 126 bytes = getDigest().digest(bytes); 127 128 StringBuffer result = new StringBuffer (); 130 131 for (int i = 0; i < bytes.length; i++) 132 { 133 byte b1 = (byte)((bytes[i] & 0xf0) >> 4); 134 byte b2 = (byte)(bytes[i] & 0x0f); 135 136 if (b1 < 10) 137 { 138 result.append((char)('0' + b1)); 139 } 140 else 141 { 142 result.append((char)('A' + (b1 - 10))); 143 } 144 if (b2 < 10) 145 { 146 result.append((char)('0' + b2)); 147 } 148 else 149 { 150 result.append((char)('A' + (b2 - 10))); 151 } 152 } 153 return (result.toString()); 154 } 155 156 161 public static synchronized Long generateSession() 162 { 163 return new Long (getRandom().nextLong()); 164 } 165 166 173 private static synchronized MessageDigest getDigest() 174 { 175 if (digest == null) 176 { 177 try 178 { 179 digest = MessageDigest.getInstance(algorithm); 180 } 181 catch (NoSuchAlgorithmException e) 182 { 183 try 184 { 185 digest = MessageDigest.getInstance(DEFAULT_ALGORITHM); 186 } 187 catch (NoSuchAlgorithmException f) 188 { 189 digest = null; 190 } 191 } 192 } 193 return (digest); 194 } 195 196 203 private static synchronized Random getRandom() 204 { 205 if (random == null) 206 { 207 try 208 { 209 Class clazz = Class.forName(randomClass); 210 211 random = (Random )clazz.newInstance(); 212 long seed = System.currentTimeMillis(); 213 char entropy[] = getEntropy().toCharArray(); 214 215 for (int i = 0; i < entropy.length; i++) 216 { 217 long update = ((byte)entropy[i]) << ((i % 8) * 8); 218 219 seed ^= update; 220 } 221 random.setSeed(seed); 222 } 223 catch (Exception e) 224 { 225 random = new java.util.Random (); 226 } 227 } 228 return (random); 229 } 230 231 236 private static String getEntropy() 237 { 238 if (null == thisHost) 239 { 240 try 241 { 242 thisHost = java.net.InetAddress.getLocalHost().getHostName(); 243 } 244 catch (java.net.UnknownHostException e) 245 { 246 log.error(Messages.getMessage("javaNetUnknownHostException00"), 247 e); 248 thisHost = "localhost"; 249 } 250 } 251 StringBuffer s = new StringBuffer (); 252 253 s.append(s.hashCode()).append('.').append(System.currentTimeMillis()) 255 .append(".AXIS@").append(thisHost); 256 return s.toString(); 257 } 258 } 259 | Popular Tags |