Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/TestDSA2.java
38855 views
/*1* Copyright (c) 2012, 2015, 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*/22/*23* @test24* @bug 808046225* @library ..26* @modules jdk.crypto.cryptoki27* @run main/othervm/timeout=250 TestDSA228* @summary verify that DSA signature works using SHA-2 digests.29* @key randomness30*/313233import java.security.*;34import java.security.spec.*;35import java.security.interfaces.*;3637public class TestDSA2 extends PKCS11Test {3839private static final String[] SIG_ALGOS = {40"SHA224withDSA",41"SHA256withDSA",42//"SHA384withDSA",43//"SHA512withDSA",44};4546private static final int KEYSIZE = 2048;4748public static void main(String[] args) throws Exception {49main(new TestDSA2(), args);50}5152@Override53public void main(Provider p) throws Exception {54KeyPair kp;55try {56KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);57kpg.initialize(KEYSIZE);58kp = kpg.generateKeyPair();59} catch (Exception ex) {60System.out.println("Skip due to no 2048-bit DSA support: " + ex);61ex.printStackTrace();62return;63}6465for (String sigAlg : SIG_ALGOS) {66test(sigAlg, kp, p);67}68}6970private static void test(String sigAlg, KeyPair kp, Provider p)71throws Exception {72Signature sig;73try {74sig = Signature.getInstance(sigAlg, p);75} catch (Exception ex) {76System.out.println("Skip due to no support: " + sigAlg);77ex.printStackTrace();78return;79}8081byte[] data = "anything will do".getBytes();8283sig.initSign(kp.getPrivate());84sig.update(data);85byte[] signature = sig.sign();8687sig.initVerify(kp.getPublic());88sig.update(data);89boolean verifies = sig.verify(signature);90System.out.println(sigAlg + ": Passed");91}92}939495