01: /*
02: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: * All rights reserved.
04: * [See end of file]
05: */
06:
07: package com.hp.hpl.jena.shared.uuid;
08:
09: import java.net.InetAddress;
10: import java.net.UnknownHostException;
11: import java.security.SecureRandom;
12: import java.util.Random;
13:
14: import com.hp.hpl.jena.JenaRuntime;
15:
16: class LibUUID {
17: //static boolean warningSent = false ;
18: //private static boolean noRandWarningSent = false ;
19:
20: static Random makeRandom() {
21: SecureRandom sRandom = new SecureRandom(); // SecureRandom.getInstance("SHA1PRNG");
22:
23: // ---- Seeding.
24: // If no setSeed() call is made before a nextBytes call, the
25: // generator "self seeds". If a setSeed() is called before
26: // any nextBytes call, no self seeding is done. We use the
27: // self seeding and our own bytes.
28:
29: // Access the internal seed generator.
30: byte[] seed1 = sRandom.generateSeed(16);
31: byte[] seed2 = LibUUID.makeSeed();
32: // seeds are cumulative
33: sRandom.setSeed(seed1);
34: sRandom.setSeed(seed2);
35: return sRandom;
36: }
37:
38: static byte[] makeSeed() {
39: // Make a random number seed from various pieces of information.
40: // One thing that is missing is something related to the identify
41: // of this OS process (so two identical programs, starting at
42: // exactly the same time, might get the same seed).
43:
44: StringBuffer seedInput = new StringBuffer(200);
45:
46: try {
47: seedInput.append(InetAddress.getLocalHost()
48: .getHostAddress());
49: }
50: // Not every machine has an IP address.
51: catch (UnknownHostException ex) {
52: }
53:
54: seedInput.append(JenaRuntime.getSystemProperty("os.version"));
55: seedInput.append(JenaRuntime.getSystemProperty("user.name"));
56: seedInput.append(JenaRuntime.getSystemProperty("java.version"));
57: seedInput.append(Integer.toString(Thread.activeCount()));
58: seedInput.append(Long.toString(Runtime.getRuntime()
59: .freeMemory()));
60: seedInput.append(Long.toString(Runtime.getRuntime()
61: .totalMemory()));
62: seedInput.append(Long.toString(System.currentTimeMillis()));
63: // Some heap variance. Maybe.
64: seedInput.append(Long.toString(new Object().hashCode()));
65: return seedInput.toString().getBytes();
66: }
67:
68: }
69:
70: /*
71: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
72: * All rights reserved.
73: *
74: * Redistribution and use in source and binary forms, with or without
75: * modification, are permitted provided that the following conditions
76: * are met:
77: * 1. Redistributions of source code must retain the above copyright
78: * notice, this list of conditions and the following disclaimer.
79: * 2. Redistributions in binary form must reproduce the above copyright
80: * notice, this list of conditions and the following disclaimer in the
81: * documentation and/or other materials provided with the distribution.
82: * 3. The name of the author may not be used to endorse or promote products
83: * derived from this software without specific prior written permission.
84: *
85: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
86: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
87: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
88: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
89: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
90: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
91: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
92: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
93: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
94: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95: */
|