Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/security/mscapi/PRNG.java
32288 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.mscapi;2627import java.security.ProviderException;28import java.security.SecureRandomSpi;2930/**31* Native PRNG implementation for Windows using the Microsoft Crypto API.32*33* @since 1.634*/3536public final class PRNG extends SecureRandomSpi37implements java.io.Serializable {3839private static final long serialVersionUID = 4129268715132691532L;4041/*42* The CryptGenRandom function fills a buffer with cryptographically random43* bytes.44*/45private static native byte[] generateSeed(int length, byte[] seed);4647/**48* Creates a random number generator.49*/50public PRNG() {51}5253/**54* Reseeds this random object. The given seed supplements, rather than55* replaces, the existing seed. Thus, repeated calls are guaranteed56* never to reduce randomness.57*58* @param seed the seed.59*/60@Override61protected void engineSetSeed(byte[] seed) {62if (seed != null) {63generateSeed(-1, seed);64}65}6667/**68* Generates a user-specified number of random bytes.69*70* @param bytes the array to be filled in with random bytes.71*/72@Override73protected void engineNextBytes(byte[] bytes) {74if (bytes != null) {75if (generateSeed(0, bytes) == null) {76throw new ProviderException("Error generating random bytes");77}78}79}8081/**82* Returns the given number of seed bytes. This call may be used to83* seed other random number generators.84*85* @param numBytes the number of seed bytes to generate.86*87* @return the seed bytes.88*/89@Override90protected byte[] engineGenerateSeed(int numBytes) {91byte[] seed = generateSeed(numBytes, null);9293if (seed == null) {94throw new ProviderException("Error generating seed bytes");95}96return seed;97}98}99100101