1 21 22 package net.sourceforge.jcetaglib.lib; 23 24 import net.sourceforge.jcetaglib.exceptions.CryptoException; 25 import org.bouncycastle.jce.provider.BouncyCastleProvider; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.security.SecureRandom ; 30 import java.security.Security ; 31 32 40 public class Seed { 41 42 47 private static String getDevRandomFilename() { 48 String filename = "/dev/urandom"; 49 50 if (System.getProperty("java.security.egd") != null) { 51 filename = System.getProperty("java.security.egd"); 52 } 53 54 return filename; 55 } 56 57 64 public static SecureRandom getSecureRandom(byte[] seed) 65 throws CryptoException { 66 SecureRandom sr = null; 67 68 Security.addProvider(new BouncyCastleProvider()); 69 70 try { 71 sr = SecureRandom.getInstance("SHA1PRNG", "SUN"); 72 73 if (seed != null && !seed.equals("")) { 74 sr.setSeed(seed); 76 Clean.blank(seed); 77 } else { 78 if (new File (getDevRandomFilename()).exists()) { 80 byte[] salt = new byte[8192]; 81 new FileInputStream (getDevRandomFilename()).read(salt); 82 sr.setSeed(salt); 83 Clean.blank(salt); 84 salt = null; 85 } 86 } 88 89 } catch (Exception e) { 90 e.printStackTrace(); 91 throw new CryptoException(e.getMessage()); 92 } 93 94 return sr; 95 } 96 } 97 | Popular Tags |