Path: blob/master/test/jdk/sun/security/provider/MessageDigest/SHA512.java
41154 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 java.security.MessageDigest;26import java.util.Arrays;2728/**29* @test30* @bug 805140831* @library /test/lib32* @summary testing SHA-512/224 and SHA-512/256.33*/34public class SHA512 {35public static void main(String[] args) throws Exception {3637MessageDigest md;3839// Test vectors obtained from40// http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_224.pdf41md = MessageDigest.getInstance("SHA-512/224");42Asserts.assertTrue(Arrays.equals(md.digest("abc".getBytes()),43xeh("4634270F 707B6A54 DAAE7530 460842E2 0E37ED26 5CEEE9A4 3E8924AA")));44Asserts.assertTrue(Arrays.equals(md.digest((45"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +46"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu").getBytes()),47xeh("23FEC5BB 94D60B23 30819264 0B0C4533 35D66473 4FE40E72 68674AF9")));4849// Test vectors obtained from50// http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf51md = MessageDigest.getInstance("SHA-512/256");52Asserts.assertTrue(Arrays.equals(md.digest("abc".getBytes()),53xeh("53048E26 81941EF9 9B2E29B7 6B4C7DAB E4C2D0C6 34FC6D46 E0E2F131 07E7AF23")));54Asserts.assertTrue(Arrays.equals(md.digest((55"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" +56"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu").getBytes()),57xeh("3928E184 FB8690F8 40DA3988 121D31BE 65CB9D3E F83EE614 6FEAC861 E19B563A")));58}5960static byte[] xeh(String in) {61in = in.replaceAll(" ", "");62int len = in.length() / 2;63byte[] out = new byte[len];64for (int i = 0; i < len; i++) {65out[i] = (byte)Integer.parseInt(in.substring(i * 2, i * 2 + 2), 16);66}67return out;68}69}707172