|
1 /* |
|
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import jdk.testlibrary.Asserts; |
|
25 |
|
26 import java.security.MessageDigest; |
|
27 import java.util.Arrays; |
|
28 |
|
29 /** |
|
30 * @test |
|
31 * @bug 8051408 |
|
32 * @library /lib/testlibrary |
|
33 * @summary testing SHA-512/224 and SHA-512/256. |
|
34 */ |
|
35 public class SHA512 { |
|
36 public static void main(String[] args) throws Exception { |
|
37 |
|
38 MessageDigest md; |
|
39 |
|
40 // Test vectors obtained from |
|
41 // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_224.pdf |
|
42 md = MessageDigest.getInstance("SHA-512/224"); |
|
43 Asserts.assertTrue(Arrays.equals(md.digest("abc".getBytes()), |
|
44 xeh("4634270F 707B6A54 DAAE7530 460842E2 0E37ED26 5CEEE9A4 3E8924AA"))); |
|
45 Asserts.assertTrue(Arrays.equals(md.digest(( |
|
46 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + |
|
47 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu").getBytes()), |
|
48 xeh("23FEC5BB 94D60B23 30819264 0B0C4533 35D66473 4FE40E72 68674AF9"))); |
|
49 |
|
50 // Test vectors obtained from |
|
51 // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf |
|
52 md = MessageDigest.getInstance("SHA-512/256"); |
|
53 Asserts.assertTrue(Arrays.equals(md.digest("abc".getBytes()), |
|
54 xeh("53048E26 81941EF9 9B2E29B7 6B4C7DAB E4C2D0C6 34FC6D46 E0E2F131 07E7AF23"))); |
|
55 Asserts.assertTrue(Arrays.equals(md.digest(( |
|
56 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + |
|
57 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu").getBytes()), |
|
58 xeh("3928E184 FB8690F8 40DA3988 121D31BE 65CB9D3E F83EE614 6FEAC861 E19B563A"))); |
|
59 } |
|
60 |
|
61 static byte[] xeh(String in) { |
|
62 in = in.replaceAll(" ", ""); |
|
63 int len = in.length() / 2; |
|
64 byte[] out = new byte[len]; |
|
65 for (int i = 0; i < len; i++) { |
|
66 out[i] = (byte)Integer.parseInt(in.substring(i * 2, i * 2 + 2), 16); |
|
67 } |
|
68 return out; |
|
69 } |
|
70 } |