KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > SecureRandom


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.net.InetAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23
24 public class SecureRandom {
25     private static java.security.SecureRandom JavaDoc sr = null;
26
27     private static java.security.SecureRandom JavaDoc getSR() {
28         if (sr != null)
29             return sr;
30         sr = new java.security.SecureRandom JavaDoc();
31         sr.setSeed(System.currentTimeMillis());
32         try {
33             sr.setSeed(InetAddress.getLocalHost().getAddress());
34         } catch (UnknownHostException JavaDoc e) {
35             //One of the rare cases where there is nothing to do
36
}
37         return sr;
38     }
39
40     public static void setSeed(long rs) {
41         getSR().setSeed(rs);
42     }
43
44     public static void setSeed(byte[] rs) {
45         getSR().setSeed(rs);
46     }
47
48     public static int getInt() {
49         return getSR().nextInt();
50     }
51
52     /**
53      * Returns a random number between 0 (inclusive) and the specified value
54      * (exclusive).
55      * @param n the bound on the random number. Must be positive.
56      * @return the random number.
57      */

58     public static int getInt(int n) {
59         return getSR().nextInt(n);
60     }
61
62     public static long getLong() {
63         return getSR().nextLong();
64     }
65 }
66
Popular Tags