J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * 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 |
| 38 | implements 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("Error generating random bytes"); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the given number of seed bytes. This call may be used to |
| 83 | * seed other random number generators. |
| 84 | * |
| 85 | * @param numBytes the number of seed bytes to generate. |
| 86 | * |
| 87 | * @return the seed bytes. |
| 88 | */ |
| 89 | protected byte[] engineGenerateSeed(int numBytes) { |
| 90 | byte[] seed = generateSeed(numBytes, null); |
| 91 | |
| 92 | if (seed == null) { |
| 93 | throw new ProviderException("Error generating seed bytes"); |
| 94 | } |
| 95 | return seed; |
| 96 | } |
| 97 | } |