KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > lib > Seed


1 /*
2   Name: Keystore.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18
19   (C) Copyright 2003 Gert Van Ham
20 */

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 JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.security.SecureRandom JavaDoc;
30 import java.security.Security JavaDoc;
31
32 /**
33  * Create SecureRandom seed from different sources
34  *
35  * @author Gert Van Ham
36  * @author hamgert@users.sourceforge.net
37  * @author http://jcetaglib.sourceforge.net
38  * @version $Id: Seed.java,v 1.1 2003/12/28 19:04:26 hamgert Exp $
39  */

40 public class Seed {
41
42     /**
43      * Get Random filename
44      *
45      * @return filename
46      */

47     private static String JavaDoc getDevRandomFilename() {
48         String JavaDoc 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     /**
58      * Create and seed SecureRandom (from optional seed, random file or self-seed)
59      *
60      * @param seed optional seed
61      * @return SecureRandom object
62      * @throws CryptoException
63      */

64     public static SecureRandom JavaDoc getSecureRandom(byte[] seed)
65             throws CryptoException {
66         SecureRandom JavaDoc 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                 // set provided seed
75
sr.setSeed(seed);
76                 Clean.blank(seed);
77             } else {
78                 // try random file name on Unix boxes
79
if (new File JavaDoc(getDevRandomFilename()).exists()) {
80                     byte[] salt = new byte[8192];
81                     new FileInputStream JavaDoc(getDevRandomFilename()).read(salt);
82                     sr.setSeed(salt);
83                     Clean.blank(salt);
84                     salt = null;
85                 }
86                 // Windows machines must self-seed
87
}
88
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91             throw new CryptoException(e.getMessage());
92         }
93
94         return sr;
95     }
96 }
97
Popular Tags