01: /*
02: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.security.mscapi;
27:
28: import java.security.ProviderException;
29: import java.security.SecureRandomSpi;
30:
31: /**
32: * Native PRNG implementation for Windows using the Microsoft Crypto API.
33: *
34: * @since 1.6
35: */
36:
37: public final class PRNG extends SecureRandomSpi implements
38: java.io.Serializable {
39:
40: // TODO - generate the serialVersionUID
41: //private static final long serialVersionUID = XXX;
42:
43: /*
44: * The CryptGenRandom function fills a buffer with cryptographically random
45: * bytes.
46: */
47: private static native byte[] generateSeed(int length, byte[] seed);
48:
49: /**
50: * Creates a random number generator.
51: */
52: public PRNG() {
53: }
54:
55: /**
56: * Reseeds this random object. The given seed supplements, rather than
57: * replaces, the existing seed. Thus, repeated calls are guaranteed
58: * never to reduce randomness.
59: *
60: * @param seed the seed.
61: */
62: protected void engineSetSeed(byte[] seed) {
63: if (seed != null) {
64: generateSeed(-1, seed);
65: }
66: }
67:
68: /**
69: * Generates a user-specified number of random bytes.
70: *
71: * @param bytes the array to be filled in with random bytes.
72: */
73: protected void engineNextBytes(byte[] bytes) {
74: if (bytes != null) {
75: if (generateSeed(0, bytes) == null) {
76: throw new ProviderException(
77: "Error generating random bytes");
78: }
79: }
80: }
81:
82: /**
83: * Returns the given number of seed bytes. This call may be used to
84: * seed other random number generators.
85: *
86: * @param numBytes the number of seed bytes to generate.
87: *
88: * @return the seed bytes.
89: */
90: protected byte[] engineGenerateSeed(int numBytes) {
91: byte[] seed = generateSeed(numBytes, null);
92:
93: if (seed == null) {
94: throw new ProviderException("Error generating seed bytes");
95: }
96: return seed;
97: }
98: }
|