1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.security.MessageDigest;
25import java.security.NoSuchAlgorithmException;
26import java.security.NoSuchProviderException;
27import java.util.Arrays;
28import java.util.List;
29
30/*
31 * @test
32 * @bug 8075286
33 * @summary Test the SHA algorithm OIDs in JDK.
34 *          OID and algorithm transformation string should match.
35 *          Both could be able to be used to generate the algorithm instance.
36 * @run main TestSHAOids
37 */
38public class TestSHAOids {
39
40    private static final String PROVIDER_NAME = "SUN";
41    private static final byte[] INPUT = "1234567890".getBytes();
42
43    private static final List<DataTuple> DATA = Arrays.asList(
44            new DataTuple("2.16.840.1.101.3.4.2.1", "SHA-256"),
45            new DataTuple("2.16.840.1.101.3.4.2.2", "SHA-384"),
46            new DataTuple("2.16.840.1.101.3.4.2.3", "SHA-512"),
47            new DataTuple("2.16.840.1.101.3.4.2.4", "SHA-224"));
48
49    public static void main(String[] args) throws Exception {
50        for (DataTuple dataTuple : DATA) {
51            runTest(dataTuple);
52            System.out.println("passed");
53        }
54        System.out.println("All tests passed");
55    }
56
57    private static void runTest(DataTuple dataTuple)
58            throws NoSuchAlgorithmException, NoSuchProviderException {
59        MessageDigest mdAlgorithm = MessageDigest.getInstance(
60                dataTuple.algorithm, PROVIDER_NAME);
61        MessageDigest mdOid = MessageDigest.getInstance(dataTuple.oid,
62                PROVIDER_NAME);
63
64        if (mdAlgorithm == null) {
65            throw new RuntimeException(String.format(
66                    "Test failed: algorithm string %s getInstance failed.%n",
67                    dataTuple.algorithm));
68        }
69
70        if (mdOid == null) {
71            throw new RuntimeException(
72                    String.format("Test failed: OID %s getInstance failed.%n",
73                            dataTuple.oid));
74        }
75
76        if (!mdAlgorithm.getAlgorithm().equals(dataTuple.algorithm)) {
77            throw new RuntimeException(String.format(
78                    "Test failed: algorithm string %s getInstance doesn't "
79                            + "generate expected algorithm.%n",
80                    dataTuple.algorithm));
81        }
82
83        mdAlgorithm.update(INPUT);
84        mdOid.update(INPUT);
85
86        // Comparison
87        if (!Arrays.equals(mdAlgorithm.digest(), mdOid.digest())) {
88            throw new RuntimeException("Digest comparison failed: "
89                    + "the two digests are not the same");
90        }
91    }
92
93    private static class DataTuple {
94
95        private final String oid;
96        private final String algorithm;
97
98        private DataTuple(String oid, String algorithm) {
99            this.oid = oid;
100            this.algorithm = algorithm;
101        }
102    }
103}
104