1 28 29 package com.idaremedia.antx.helpers; 30 31 import java.security.NoSuchAlgorithmException ; 32 import java.security.SecureRandom ; 33 34 54 55 public final class SIDs 56 { 57 58 public static final String SECURERANDOM_ALGORITHM_PROPERTY= 59 "jware.securerandom.algo"; 60 61 62 63 private static SecureRandom getSRNG(String algo) 64 { 65 SecureRandom srng=null; 66 try { 67 srng= SecureRandom.getInstance(algo); 68 } catch (NoSuchAlgorithmException algox) { 69 return null; 70 } 71 srng.setSeed(System.currentTimeMillis()); 72 return srng; 73 } 74 75 76 78 private static final SecureRandom PRNG; 79 static { 80 SecureRandom srng=null; 81 String preferred = System.getProperty(SECURERANDOM_ALGORITHM_PROPERTY); 82 if (preferred!=null) { 83 srng = getSRNG(preferred); 84 if (srng==null) { 85 throw new RuntimeException ("Missing SPRNG algorithm: "+preferred); 86 } 87 } else { 88 srng = getSRNG("SHA1PRNG"); if (srng==null) { 90 srng = getSRNG("IBMSecureRandom"); if (srng==null) { 92 throw new RuntimeException ("Missing SPRNG algorithm: SHA1PRNG"); 93 } 94 } 95 } 96 PRNG= srng; 97 } 98 99 100 101 private static StringBuffer m_sb= new StringBuffer (32); 102 103 104 107 public interface Finder { 108 public boolean exists(String sid); 109 } 110 111 112 116 public static long nextLong() 117 { 118 return PRNG.nextLong(); 119 } 120 121 122 126 public static int nextInt() 127 { 128 return PRNG.nextInt(); 129 } 130 131 132 138 public static int nextInt(int limit) 139 { 140 return PRNG.nextInt(limit); 141 } 142 143 144 148 public static String next() 149 { 150 synchronized(PRNG) { 151 m_sb.setLength(0); 152 m_sb.append(o_SYSID); 153 m_sb.append(PRNG.nextInt(Integer.MAX_VALUE)); m_sb.append(c_SYSID); 155 return m_sb.substring(0); } 157 } 158 159 160 166 public static String next(String pfx) 167 { 168 if (pfx==null) { 169 throw new IllegalArgumentException (); 170 } 171 synchronized(PRNG) { 172 m_sb.setLength(0); 173 m_sb.append(pfx); 174 m_sb.append(PRNG.nextInt(999999)); 175 return m_sb.substring(0); } 177 } 178 179 180 186 public static String next(Finder uv) 187 { 188 if (uv==null) { 189 throw new IllegalArgumentException (); 190 } 191 synchronized(PRNG) { 192 String s; 193 do { 194 s= null; 195 m_sb.setLength(0); 196 m_sb.append(PRNG.nextInt(999999999)); 197 s = m_sb.substring(0); 198 } while (uv.exists(s)); 199 return s; } 201 } 202 203 204 212 public static String next(Finder uv, String pfx) 213 { 214 if (uv==null || pfx==null) { 215 throw new IllegalArgumentException (); 216 } 217 synchronized(PRNG) { 218 String s; 219 do { 220 s= null; 221 m_sb.setLength(0); 222 m_sb.append(pfx); 223 m_sb.append(PRNG.nextInt(999999)); 224 s = m_sb.substring(0); 225 } while (uv.exists(s)); 226 return s; } 228 } 229 230 231 239 public static final byte[] nextSeed(int numBytes) 240 { 241 return PRNG.generateSeed(numBytes); 242 } 243 244 245 246 private SIDs() 247 {} 248 249 250 private static final String o_SYSID= "RUT{"; 251 private static final String c_SYSID= "}"; 252 } 253 254 255 | Popular Tags |