Path: blob/master/test/jdk/sun/security/provider/NSASuiteB/TestSHAOids.java
41153 views
/*1* Copyright (c) 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*/2223import java.security.MessageDigest;24import java.security.NoSuchAlgorithmException;25import java.security.NoSuchProviderException;26import java.util.Arrays;27import java.util.List;2829/*30* @test31* @bug 807528632* @summary Test the SHA algorithm OIDs in JDK.33* OID and algorithm transformation string should match.34* Both could be able to be used to generate the algorithm instance.35* @run main TestSHAOids36*/37public class TestSHAOids {3839private static final String PROVIDER_NAME = "SUN";40private static final byte[] INPUT = "1234567890".getBytes();4142private static final List<DataTuple> DATA = Arrays.asList(43new DataTuple("2.16.840.1.101.3.4.2.1", "SHA-256"),44new DataTuple("2.16.840.1.101.3.4.2.2", "SHA-384"),45new DataTuple("2.16.840.1.101.3.4.2.3", "SHA-512"),46new DataTuple("2.16.840.1.101.3.4.2.4", "SHA-224"));4748public static void main(String[] args) throws Exception {49for (DataTuple dataTuple : DATA) {50runTest(dataTuple);51System.out.println("passed");52}53System.out.println("All tests passed");54}5556private static void runTest(DataTuple dataTuple)57throws NoSuchAlgorithmException, NoSuchProviderException {58MessageDigest mdAlgorithm = MessageDigest.getInstance(59dataTuple.algorithm, PROVIDER_NAME);60MessageDigest mdOid = MessageDigest.getInstance(dataTuple.oid,61PROVIDER_NAME);6263if (mdAlgorithm == null) {64throw new RuntimeException(String.format(65"Test failed: algorithm string %s getInstance failed.%n",66dataTuple.algorithm));67}6869if (mdOid == null) {70throw new RuntimeException(71String.format("Test failed: OID %s getInstance failed.%n",72dataTuple.oid));73}7475if (!mdAlgorithm.getAlgorithm().equals(dataTuple.algorithm)) {76throw new RuntimeException(String.format(77"Test failed: algorithm string %s getInstance doesn't "78+ "generate expected algorithm.%n",79dataTuple.algorithm));80}8182mdAlgorithm.update(INPUT);83mdOid.update(INPUT);8485// Comparison86if (!Arrays.equals(mdAlgorithm.digest(), mdOid.digest())) {87throw new RuntimeException("Digest comparison failed: "88+ "the two digests are not the same");89}90}9192private static class DataTuple {9394private final String oid;95private final String algorithm;9697private DataTuple(String oid, String algorithm) {98this.oid = oid;99this.algorithm = algorithm;100}101}102}103104105