Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/Utils.java
41159 views
/*1* Copyright (c) 1998, 2014, 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.SecureRandom;24import javax.crypto.spec.SecretKeySpec;2526/**27* Helper class.28*/29class Utils {3031static final int KEY_SIZE = 70;3233static final String[] MAC_ALGOS = {"HmacMD5", "HmacSHA1", "HmacSHA224",34"HmacSHA256", "HmacSHA384", "HmacSHA512"};3536/**37* Get SecretKeySpec.38*/39static SecretKeySpec getSecretKeySpec() {40SecureRandom srdm = new SecureRandom();41byte[] keyVal = new byte[KEY_SIZE];42srdm.nextBytes(keyVal);43return new SecretKeySpec(keyVal, "HMAC");44}4546static void runTests(MacTest... tests) {47boolean success = true;48for (MacTest test : tests) {49success &= runTest(test);50}5152if (success) {53System.out.println("Test passed");54} else {55throw new RuntimeException("Test failed");56}57}5859private static boolean runTest(MacTest test) {60boolean success = true;61for (String alg : MAC_ALGOS) {62try {63System.out.println("Test " + alg);64test.doTest(alg);65} catch (Exception e) {66System.out.println("Unexpected exception:");67e.printStackTrace();68success = false;69}70}7172return success;73}74}7576interface MacTest {77void doTest(String alg) throws Exception;78}798081