1/* 2 * Copyright (c) 2002, 2016, 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 24/** 25 * @test 26 * @bug 4775971 27 * @summary test the clone implementation of SHA, SHA-224, SHA-256, 28 * SHA-384, SHA-512 MessageDigest implementation. 29 */ 30import java.security.*; 31import java.util.*; 32 33public class TestSHAClone { 34 35 // OracleUcrypto provider gets its digest impl from either 36 // libucrypto (starting S12 with SHA-3 support added) and 37 // libmd (pre-S12, no SHA-3 at all). 38 // The impls from libucrypto does not support clone but ones 39 // from libmd do. 40 private static final String[] ALGOS = { 41 "SHA", "SHA-224", "SHA-256", "SHA-384", "SHA-512" 42 }; 43 44 private static byte[] input1 = { 45 (byte)0x1, (byte)0x2, (byte)0x3 46 }; 47 48 private static byte[] input2 = { 49 (byte)0x4, (byte)0x5, (byte)0x6 50 }; 51 52 private MessageDigest md; 53 54 private TestSHAClone(String algo, Provider p) throws Exception { 55 md = MessageDigest.getInstance(algo, p); 56 } 57 58 private void run() throws Exception { 59 md.update(input1); 60 MessageDigest md2; 61 try { 62 md2 = (MessageDigest) md.clone(); 63 } catch (CloneNotSupportedException cnse) { 64 System.out.println(md.getAlgorithm() + ": clone unsupported"); 65 return; 66 } 67 md.update(input2); 68 md2.update(input2); 69 if (!Arrays.equals(md.digest(), md2.digest())) { 70 throw new Exception(md.getAlgorithm() + ": comparison failed"); 71 } else { 72 System.out.println(md.getAlgorithm() + ": passed"); 73 } 74 } 75 76 77 public static void main(String[] argv) throws Exception { 78 Provider p = Security.getProvider("SUN"); 79 for (int i=0; i<ALGOS.length; i++) { 80 TestSHAClone test = new TestSHAClone(ALGOS[i], p); 81 test.run(); 82 } 83 } 84} 85