Path: blob/master/test/jdk/sun/security/provider/SecureRandom/SHA1PRNGReseed.java
41155 views
/*1* Copyright (c) 2016, 2021, 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*/22import java.io.ByteArrayInputStream;23import java.io.ByteArrayOutputStream;24import java.io.ObjectInputStream;25import java.io.ObjectOutputStream;26import java.security.SecureRandom;2728/**29* @test30* @bug 8154523 824789531* @summary SHA1PRNG output should change after setSeed32*/33public class SHA1PRNGReseed {3435public static void main(String[] args) throws Exception {36SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");37sr.setSeed(1);38sr.nextInt();3940ByteArrayOutputStream bout = new ByteArrayOutputStream();41new ObjectOutputStream(bout).writeObject(sr);4243SecureRandom sr2 = (SecureRandom) new ObjectInputStream(44new ByteArrayInputStream(bout.toByteArray())).readObject();4546int i1 = sr.nextInt();47sr2.setSeed(2);48int i2 = sr2.nextInt();4950if (i1 == i2) {51throw new Exception("output should not be the same");52}53}54}555657