Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/HmacMD5.java
41159 views
/*1* Copyright (c) 1998, 2015, 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;4041byte[][][] test_data = {42{43{ (byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,44(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,45(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b,46(byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b }, // key147"Hi There".getBytes(), // data148{ (byte)0x92, (byte)0x94, (byte)0x72, (byte)0x7a,49(byte)0x36, (byte)0x38, (byte)0xbb, (byte)0x1c,50(byte)0x13, (byte)0xf4, (byte)0x8e, (byte)0xf8,51(byte)0x15, (byte)0x8b, (byte)0xfc, (byte)0x9d // result152}53},54{55"Jefe".getBytes(), // key256"what do ya want for nothing?".getBytes(), // data257{ (byte)0x75, (byte)0x0c, (byte)0x78, (byte)0x3e,58(byte)0x6a, (byte)0xb0, (byte)0xb5, (byte)0x03,59(byte)0xea, (byte)0xa8, (byte)0x6e, (byte)0x31,60(byte)0x0a, (byte)0x5d, (byte)0xb7, (byte)0x38 // result261}62},63{64{ (byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,65(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,66(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA,67(byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA // key368},69{ (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,70(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,71(byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD,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 // data382},83{ (byte)0x56, (byte)0xbe, (byte)0x34, (byte)0x52,84(byte)0x1d, (byte)0x14, (byte)0x4c, (byte)0x88,85(byte)0xdb, (byte)0xb8, (byte)0xc7, (byte)0x33,86(byte)0xf0, (byte)0xe8, (byte)0xb3, (byte)0xf6 // result387}88}89};9091mac = Mac.getInstance("HmacMD5", "SunJCE");92for (i=0; i<3; i++) {93j=0;9495mac.init(new SecretKeySpec(test_data[i][j++], "HMAC"));96byte[] result = mac.doFinal(test_data[i][j++]);97if (result.length != test_data[i][j].length) {98throw new Exception("Different result length");99}100for (n=0; n<result.length; n++) {101if (result[n] != test_data[i][j][n]) {102throw new Exception("Different");103}104}105}106107// now test multiple-part operation, using the 2nd test vector108mac = Mac.getInstance("HmacMD5", "SunJCE");109mac.init(new SecretKeySpec("Jefe".getBytes(), "HMAC"));110mac.update("what do ya ".getBytes());111mac.update("want for ".getBytes());112mac.update("nothing?".getBytes());113byte[] result = mac.doFinal();114if (result.length != test_data[1][2].length) {115throw new Exception("Different result length");116}117for (i=0; i<result.length; i++) {118if (result[i] != test_data[1][2][i]) {119throw new Exception("Different");120}121}122123System.out.println("Test SUCCEEDED");124}125}126127128