Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/ReinitDigest.java
41153 views
1
/*
2
* Copyright (c) 2003, 2020, 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
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
36
import java.security.MessageDigest;
37
import java.security.Provider;
38
import java.util.Arrays;
39
import java.util.Random;
40
import java.util.List;
41
42
public class ReinitDigest extends PKCS11Test {
43
44
public static void main(String[] args) throws Exception {
45
main(new ReinitDigest(), args);
46
}
47
48
@Override
49
public void main(Provider p) throws Exception {
50
List<String> ALGS = getSupportedAlgorithms("MessageDigest",
51
"SHA", p);
52
Random r = new Random();
53
byte[] data1 = new byte[10 * 1024];
54
byte[] data2 = new byte[10 * 1024];
55
r.nextBytes(data1);
56
r.nextBytes(data2);
57
58
boolean success = true;
59
for (String alg : ALGS) {
60
try {
61
doTest(alg, p, data1, data2);
62
} catch (Exception e) {
63
System.out.println("Unexpected exception: " + e);
64
e.printStackTrace();
65
success = false;
66
}
67
}
68
69
if (!success) {
70
throw new RuntimeException("Test failed");
71
}
72
System.out.println("All tests passed");
73
}
74
75
private void doTest(String alg, Provider p, byte[] data1, byte[] data2)
76
throws Exception {
77
System.out.println("Testing " + alg);
78
MessageDigest md = MessageDigest.getInstance(alg, "SUN");
79
byte[] d1 = md.digest(data1);
80
md = MessageDigest.getInstance(alg, p);
81
byte[] d2 = md.digest(data1);
82
check(d1, d2);
83
byte[] d3 = md.digest(data1);
84
check(d1, d3);
85
md.update(data2);
86
md.update((byte)0);
87
md.reset();
88
byte[] d4 = md.digest(data1);
89
check(d1, d4);
90
}
91
92
private static void check(byte[] d1, byte[] d2) throws Exception {
93
if (Arrays.equals(d1, d2) == false) {
94
throw new RuntimeException("Digest mismatch");
95
}
96
}
97
}
98
99