1 2 24 25 26 27 28 29 package com.lutris.appserver.server.sessionEnhydra; 30 31 import java.io.ByteArrayOutputStream ; 32 import java.io.DataOutputStream ; 33 import java.io.IOException ; 34 import java.security.MessageDigest ; 35 import java.security.NoSuchAlgorithmException ; 36 import java.security.SecureRandom ; 37 38 import com.lutris.util.Base64Encoder; 39 import com.lutris.util.FatalExceptionError; 40 41 61 public class StandardSessionKeyGen extends Thread { 62 63 69 private SecureRandom randomizer = new SecureRandom (); 70 71 75 79 private int randomCounter = 0; 80 81 84 private long[] alarmVector; 85 private long[] intervalVector; 86 private int numIntervals; 87 88 98 public StandardSessionKeyGen(long intervals[]) { 99 int i; 100 long now = System.currentTimeMillis(); 101 numIntervals = intervals.length; 102 intervalVector = new long[numIntervals]; 103 104 for (i=0; i<numIntervals; i++) { 106 intervalVector[i] = intervals[i] * 1000; } 108 alarmVector = new long[numIntervals]; 109 for (i=0; i<numIntervals; i++) { 110 alarmVector[i] = now + intervalVector[i]; 111 } 112 setDaemon(true); } 114 115 121 public void run() { 122 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 123 DataOutputStream dos = new DataOutputStream (bos); 124 MessageDigest md5 = null; 125 int i; 126 long now, least, interval; 127 128 try { 129 md5 = MessageDigest.getInstance("MD5"); 130 } catch (NoSuchAlgorithmException e) { 131 throw new FatalExceptionError(e); 133 } 134 while (true) { 135 now = System.currentTimeMillis(); 136 least = Long.MAX_VALUE; 137 138 for (i=0; i<numIntervals; i++) { 140 if (now >= alarmVector[i]) { 141 try { 142 dos.writeLong(randomCounter); 143 dos.writeLong(now); 144 dos.flush(); 145 } catch (IOException e) { 146 throw new FatalExceptionError(e); } 148 randomizer.setSeed(md5.digest(bos.toByteArray())); 149 150 alarmVector[i] = now + intervalVector[i]; 152 } 153 154 if (alarmVector[i] < least) { 156 least = alarmVector[i]; 157 } 158 } 159 160 interval = least - now; 162 if (interval <= 0) { 163 interval = 1; 164 } 165 try { 166 sleep(interval); 167 } catch (InterruptedException e) { 168 } 170 } 171 } 172 173 179 public void incrementRandomCounter() { 180 randomCounter++; 181 } 182 183 198 public String newSessionKey() { 199 try { 200 byte[] rand32 = new byte[32]; 201 byte[] rand2 = new byte[2]; 202 byte[] md5result; 203 long now = System.currentTimeMillis(); 204 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 205 DataOutputStream dos = new DataOutputStream (bos); 206 MessageDigest md5 = MessageDigest.getInstance("MD5"); 207 randomizer.nextBytes(rand32); 208 randomizer.nextBytes(rand2); 209 dos.write(rand32, 0, rand32.length); 210 dos.writeLong(now); 211 dos.flush(); 212 md5result = md5.digest(bos.toByteArray()); 213 bos.reset(); 214 dos = new DataOutputStream (bos); 215 dos.write(md5result, 0, md5result.length); 216 dos.write(rand2, 0, rand2.length); 217 return Base64Encoder.toBase64SessionKeyString(bos.toByteArray()); 218 } catch (IOException e) { 219 throw new FatalExceptionError(e); 221 } catch (NoSuchAlgorithmException e) { 222 throw new FatalExceptionError(e); 224 } 225 } 226 227 230 public void shutdown() { 231 stop(); 232 } 233 } 234 235 | Popular Tags |