Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/HmacSHA512.java
41159 views
/*1* Copyright (c) 2016, 2018, 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 jdk.test.lib.Asserts;2425import javax.crypto.Mac;26import javax.crypto.spec.SecretKeySpec;27import java.util.Arrays;2829/**30* @test31* @bug 805140832* @library /test/lib33* @summary testing HmacSHA512/224 and HmacSHA512/256.34*/35public class HmacSHA512 {36public static void main(String[] args) throws Exception {3738Mac mac;3940// Test vectors obtained from41// https://groups.google.com/d/msg/sci.crypt/OolWgsgQD-8/IUR2KhCcfEkJ42mac = Mac.getInstance("HmacSHA512/224");43mac.init(new SecretKeySpec(xeh("4a656665"), "HmacSHA512/224"));44mac.update("what do ya want for nothing?".getBytes());45Asserts.assertTrue(Arrays.equals(mac.doFinal(),46xeh("4a530b31a79ebcce36916546317c45f247d83241dfb818fd37254bde")));4748mac = Mac.getInstance("HmacSHA512/256");49mac.init(new SecretKeySpec(xeh("4a656665"), "HmacSHA512/256"));50mac.update("what do ya want for nothing?".getBytes());51Asserts.assertTrue(Arrays.equals(mac.doFinal(),52xeh("6df7b24630d5ccb2ee335407081a87188c221489768fa2020513b2d593359456")));5354mac = Mac.getInstance("HmacSHA512/224");55mac.init(new SecretKeySpec(xeh("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),56"HmacSHA512/224"));57mac.update("Hi There".getBytes());58Asserts.assertTrue(Arrays.equals(mac.doFinal(),59xeh("b244ba01307c0e7a8ccaad13b1067a4cf6b961fe0c6a20bda3d92039")));6061mac = Mac.getInstance("HmacSHA512/256");62mac.init(new SecretKeySpec(xeh("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),63"HmacSHA512/256"));64mac.update("Hi There".getBytes());65Asserts.assertTrue(Arrays.equals(mac.doFinal(),66xeh("9f9126c3d9c3c330d760425ca8a217e31feae31bfe70196ff81642b868402eab")));67}6869static byte[] xeh(String in) {70in = in.replaceAll(" ", "");71int len = in.length() / 2;72byte[] out = new byte[len];73for (int i = 0; i < len; i++) {74out[i] = (byte)Integer.parseInt(in.substring(i * 2, i * 2 + 2), 16);75}76return out;77}78}798081