Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/MacClone.java
41159 views
/*1* Copyright (c) 1998, 2013, 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*/2223/*24* @test25* @bug 7087021 801306926* @summary Clone tests for all MAC algorithms.27* @author Jan Luehe28*/29import java.security.spec.AlgorithmParameterSpec;30import javax.crypto.*;31import javax.crypto.spec.*;3233public class MacClone {3435public static void main(String[] args) throws Exception {3637String[] algos = { "HmacMD5", "HmacSHA1", "HmacSHA224", "HmacSHA256",38"HmacSHA384", "HmacSHA512" };39KeyGenerator kgen = KeyGenerator.getInstance("DES");40SecretKey skey = kgen.generateKey();41for (String algo : algos) {42doTest(algo, skey, null);43}4445String[] algos2 = { "HmacPBESHA1", "PBEWithHmacSHA1",46"PBEWithHmacSHA224", "PBEWithHmacSHA256",47"PBEWithHmacSHA384", "PBEWithHmacSHA512" };48skey = new SecretKeySpec("whatever".getBytes(), "PBE");49PBEParameterSpec params =50new PBEParameterSpec("1234567890".getBytes(), 500);51for (String algo : algos2) {52doTest(algo, skey, params);53}54System.out.println("Test Passed");55}5657private static void doTest(String algo, SecretKey skey,58AlgorithmParameterSpec params) throws Exception {59//60// Clone an uninitialized Mac object61//62Mac mac = Mac.getInstance(algo, "SunJCE");63Mac macClone = (Mac)mac.clone();64System.out.println(macClone.getProvider().toString());65System.out.println(macClone.getAlgorithm());66boolean thrown = false;67try {68macClone.update((byte)0x12);69} catch (IllegalStateException ise) {70thrown = true;71}72if (!thrown) {73throw new Exception("Expected IllegalStateException not thrown");74}7576//77// Clone an initialized Mac object78//79mac = Mac.getInstance(algo, "SunJCE");80mac.init(skey, params);81macClone = (Mac)mac.clone();82System.out.println(macClone.getProvider().toString());83System.out.println(macClone.getAlgorithm());84mac.update((byte)0x12);85macClone.update((byte)0x12);86byte[] macFinal = mac.doFinal();87byte[] macCloneFinal = macClone.doFinal();88if (!java.util.Arrays.equals(macFinal, macCloneFinal)) {89throw new Exception("ERROR: MAC result of init clone is different");90} else System.out.println("MAC check#1 passed");9192//93// Clone an updated Mac object94//95mac.update((byte)0x12);96macClone = (Mac)mac.clone();97mac.update((byte)0x34);98macClone.update((byte)0x34);99macFinal = mac.doFinal();100macCloneFinal = macClone.doFinal();101if (!java.util.Arrays.equals(macFinal, macCloneFinal)) {102throw new Exception("ERROR: MAC result of updated clone is different");103} else System.out.println("MAC check#2 passed");104}105}106107108