Path: blob/master/test/jdk/sun/security/pkcs11/Signature/ReinitSignature.java
41153 views
/*1* Copyright (c) 2003, 2020, 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 4856966 824233226* @summary test that reinitializing Signatures works correctly27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main ReinitSignature32*/3334import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.PrivateKey;37import java.security.Provider;38import java.security.PublicKey;39import java.security.Signature;40import java.util.Random;4142public class ReinitSignature extends PKCS11Test {4344public static void main(String[] args) throws Exception {45main(new ReinitSignature());46}4748public void main(Provider p) throws Exception {4950KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);51kpg.initialize(2048);52KeyPair kp = kpg.generateKeyPair();53PrivateKey privateKey = kp.getPrivate();54PublicKey publicKey = kp.getPublic();55Signature sig = Signature.getInstance("SHA256withRSA", p);56byte[] data = new byte[10 * 1024];57new Random().nextBytes(data);58sig.initSign(privateKey);59sig.initSign(privateKey);60sig.update(data);61sig.initSign(privateKey);62sig.update(data);63byte[] signature = sig.sign();64sig.update(data);65sig.initSign(privateKey);66sig.update(data);67sig.sign();68sig.sign();69sig.initSign(privateKey);70sig.sign();7172System.out.println("All tests passed");73}7475}767778