Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Mac/HmacMD5.java
38867 views
/*1* Copyright (c) 1998, 2007, 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 000000026* @summary Checks compliance of the HMAC-MD5 implementation with27* RFC 2104, using the test vectors specified there.28* @author Jan Luehe29*/3031import java.security.*;32import javax.crypto.*;33import javax.crypto.spec.*;3435public class HmacMD5 {3637public static void main(String argv[]) throws Exception {38int i, j, n;39Mac mac;4041Provider jce = new com.sun.crypto.provider.SunJCE();42Security.addProvider(jce);4344byte[][][] test_data = {45{46{ (byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,47(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,48(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,49(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b }, // key150"Hi There".getBytes(), // data151{ (byte)0x92, (byte)0x94, (byte)0x72, (byte)0x7a,52(byte)0x36, (byte)0x38, (byte)0xbb, (byte)0x1c,53(byte)0x13, (byte)0xf4, (byte)0x8e, (byte)0xf8,54(byte)0x15, (byte)0x8b, (byte)0xfc, (byte)0x9d // result155}56},57{58"Jefe".getBytes(), // key259"what do ya want for nothing?".getBytes(), // data260{ (byte)0x75, (byte)0x0c, (byte)0x78, (byte)0x3e,61(byte)0x6a, (byte)0xb0, (byte)0xb5, (byte)0x03,62(byte)0xea, (byte)0xa8, (byte)0x6e, (byte)0x31,63(byte)0x0a, (byte)0x5d, (byte)0xb7, (byte)0x38 // result264}65},66{67{ (byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,68(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,69(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,70(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA // key371},72{ (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,73(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,74(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,75(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,76(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,77(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,78(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,79(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,80(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,81(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,82(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,83(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,84(byte)0xDD, (byte)0xDD // data385},86{ (byte)0x56, (byte)0xbe, (byte)0x34, (byte)0x52,87(byte)0x1d, (byte)0x14, (byte)0x4c, (byte)0x88,88(byte)0xdb, (byte)0xb8, (byte)0xc7, (byte)0x33,89(byte)0xf0, (byte)0xe8, (byte)0xb3, (byte)0xf6 // result390}91}92};9394mac = Mac.getInstance("HmacMD5");95for (i=0; i<3; i++) {96j=0;9798mac.init(new SecretKeySpec(test_data[i][j++], "HMAC"));99byte[] result = mac.doFinal(test_data[i][j++]);100if (result.length != test_data[i][j].length) {101throw new Exception("Different result length");102}103for (n=0; n<result.length; n++) {104if (result[n] != test_data[i][j][n]) {105throw new Exception("Different");106}107}108}109110// now test multiple-part operation, using the 2nd test vector111mac = Mac.getInstance("HmacMD5");112mac.init(new SecretKeySpec("Jefe".getBytes(), "HMAC"));113mac.update("what do ya ".getBytes());114mac.update("want for ".getBytes());115mac.update("nothing?".getBytes());116byte[] result = mac.doFinal();117if (result.length != test_data[1][2].length) {118throw new Exception("Different result length");119}120for (i=0; i<result.length; i++) {121if (result[i] != test_data[1][2][i]) {122throw new Exception("Different");123}124}125126System.out.println("Test SUCCEEDED");127}128}129130131