Path: blob/master/test/jdk/sun/security/provider/SecureRandom/StrongSeedReader.java
41154 views
/*1* Copyright (c) 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 642547726* @summary Better support for generation of high entropy random numbers27* @run main/othervm StrongSeedReader28*/2930import java.io.*;31import java.net.*;32import java.security.SecureRandom;3334/**35* A simple test which takes into account knowledge about the underlying36* implementation. This may change if the implementations change.37*38* Create a new EGD file with known bytes, then set the EGD System property. The39* data read should be the same as what was written.40*/41public class StrongSeedReader {4243public static void main(String[] args) throws Exception {44// Skip Windows, the SHA1PRNG uses CryptGenRandom.45if (System.getProperty("os.name", "unknown").startsWith("Windows")) {46return;47}4849File file = null;50try {51file = new File(System.getProperty("java.io.tmpdir"),52"StrongSeedReader.tmpdata");5354// write a bunch of 0's to the file.55FileOutputStream fos = new FileOutputStream(file);56fos.write(new byte[2048]);5758System.setProperty("java.security.egd", file.toURI().toString());59testSeed("NativePRNG");60testSeed("SHA1PRNG");61testSeed("DRBG");62} finally {63if (file != null) {64file.delete();65}66}67}6869private static void testSeed(String alg) throws Exception {70System.out.println("Testing: " + alg);71SecureRandom sr = SecureRandom.getInstance(alg);72byte[] ba = sr.generateSeed(20);7374// We should get back a bunch of zeros from the file.75for (byte b : ba) {76if (b != 0) {77throw new Exception("Byte != 0");78}79}80}81}828384