1 /* 2 * Copyright (c) 2002, 2003, 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-256, 28 * SHA-384, SHA-512 MessageDigest implementation. 29 */ 30 import java.security.*; 31 import java.util.*; 32 33 public class TestSHAClone { 34 35 private static final String[] ALGOS = { 36 "SHA", "SHA-256", "SHA-512", "SHA-384" 37 }; 38 39 private static byte[] input1 = { 40 (byte)0x1, (byte)0x2, (byte)0x3 41 }; 42 43 private static byte[] input2 = { 44 (byte)0x4, (byte)0x5, (byte)0x6 45 }; 46 47 private MessageDigest md; 48 49 private TestSHAClone(String algo, Provider p) throws Exception { 50 md = MessageDigest.getInstance(algo, p); 51 } 52 53 private void run() throws Exception { 54 md.update(input1); 55 MessageDigest md2 = (MessageDigest) md.clone(); 56 md.update(input2); 57 md2.update(input2); 58 if (!Arrays.equals(md.digest(), md2.digest())) { 59 throw new Exception(md.getAlgorithm() + ": comparison failed"); 60 } else { 61 System.out.println(md.getAlgorithm() + ": passed"); 62 } 63 } 64 65 66 public static void main(String[] argv) throws Exception { 67 Provider p = Security.getProvider("SUN"); 68 for (int i=0; i<ALGOS.length; i++) { 69 TestSHAClone test = new TestSHAClone(ALGOS[i], p); 70 test.run(); 71 } 72 } 73 }