Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/ReinitDigest.java
66646 views
1
/*
2
* Copyright (c) 2003, 2021, 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
/*
25
* @test
26
* @bug 4856966 8242332 8269276
27
* @summary
28
* @author Andreas Sterbenz
29
* @library /test/lib ..
30
* @key randomness
31
* @modules jdk.crypto.cryptoki
32
* @run main/othervm ReinitDigest
33
* @run main/othervm -Djava.security.manager=allow ReinitDigest sm
34
*/
35
import java.security.MessageDigest;
36
import java.security.Provider;
37
import java.util.Arrays;
38
import java.util.Random;
39
import java.util.List;
40
41
public class ReinitDigest extends PKCS11Test {
42
43
public static void main(String[] args) throws Exception {
44
main(new ReinitDigest(), args);
45
}
46
47
@Override
48
public void main(Provider p) throws Exception {
49
List<String> ALGS = getSupportedAlgorithms("MessageDigest", "SHA", p);
50
Random r = new Random();
51
byte[] data1 = new byte[10 * 1024];
52
byte[] data2 = new byte[10 * 1024];
53
r.nextBytes(data1);
54
r.nextBytes(data2);
55
56
boolean success = true;
57
for (String alg : ALGS) {
58
try {
59
doTest(alg, p, data1, data2);
60
} catch (Exception e) {
61
System.out.println("Unexpected exception: " + e);
62
e.printStackTrace();
63
success = false;
64
}
65
}
66
67
if (!success) {
68
throw new RuntimeException("Test failed");
69
}
70
System.out.println("All tests passed");
71
}
72
73
private void doTest(String alg, Provider p, byte[] data1, byte[] data2)
74
throws Exception {
75
System.out.println("Testing " + alg);
76
MessageDigest md1 = MessageDigest.getInstance(alg, "SUN");
77
byte[] d1 = md1.digest(data1);
78
MessageDigest md2 = MessageDigest.getInstance(alg, p);
79
checkInstances(md1, md2);
80
byte[] d2 = md2.digest(data1);
81
check(d1, d2);
82
byte[] d3 = md2.digest(data1);
83
check(d1, d3);
84
md2.update(data2);
85
md2.update((byte) 0);
86
md2.reset();
87
byte[] d4 = md2.digest(data1);
88
check(d1, d4);
89
}
90
91
private static void check(byte[] d1, byte[] d2) throws Exception {
92
if (Arrays.equals(d1, d2) == false) {
93
throw new RuntimeException("Digest mismatch");
94
}
95
}
96
97
private static void checkInstances(MessageDigest md1, MessageDigest md2)
98
throws Exception {
99
if (md1.equals(md2)) {
100
throw new RuntimeException("MD instances should be different");
101
}
102
if (!md1.getAlgorithm().equals(md2.getAlgorithm())) {
103
throw new RuntimeException("Algorithm name should equal");
104
}
105
if (md1.getProvider().getName().equals(md2.getProvider().getName())) {
106
throw new RuntimeException("Provider name should be different");
107
}
108
}
109
}
110
111