Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/DigestCloneabilityTest.java
41159 views
1
/*
2
* Copyright (c) 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 8246077
27
* @summary Ensure a cloneable digest can be accepted/used by HmacCore
28
*/
29
import java.security.*;
30
import java.security.spec.AlgorithmParameterSpec;
31
import javax.crypto.*;
32
import javax.crypto.spec.*;
33
34
public class DigestCloneabilityTest {
35
36
private static String ALGO = "HmacSHA512";
37
38
public static void main(String[] args) throws Exception {
39
Provider p = new SampleProvider();
40
// make SampleProvider the most preferred, so its digest impl is picked
41
int status = Security.insertProviderAt(p, 1);
42
try {
43
Mac mac = Mac.getInstance(ALGO, "SunJCE");
44
// do a complete mac generation and check if the supplied
45
// digest is used
46
mac.init(new SecretKeySpec(new byte[512>>3], ALGO));
47
mac.update((byte)0x12);
48
byte[] macBytes = mac.doFinal();
49
if (!SampleProvider.CloneableDigest.isUsed) {
50
throw new RuntimeException("Expected Digest impl not used");
51
}
52
} finally {
53
if (status != -1) {
54
Security.removeProvider(p.getName());
55
}
56
}
57
System.out.println("Test Passed");
58
}
59
60
public static class SampleProvider extends Provider {
61
62
public SampleProvider() {
63
super("Sample", "1.0", "test provider");
64
putService(new Provider.Service(this, "MessageDigest", "SHA-512",
65
"DigestCloneabilityTest$SampleProvider$CloneableDigest",
66
null, null));
67
}
68
public static class CloneableDigest extends MessageDigestSpi
69
implements Cloneable {
70
private MessageDigest md;
71
static boolean isUsed = false;
72
73
public CloneableDigest() throws NoSuchAlgorithmException {
74
try {
75
md = MessageDigest.getInstance("SHA-512", "SUN");
76
} catch (NoSuchProviderException nspe) {
77
// should never happen
78
}
79
}
80
81
public byte[] engineDigest() {
82
isUsed = true;
83
return md.digest();
84
}
85
86
public void engineReset() {
87
isUsed = true;
88
md.reset();
89
}
90
91
public void engineUpdate(byte input) {
92
isUsed = true;
93
md.update(input);
94
}
95
96
public void engineUpdate(byte[] b, int ofs, int len) {
97
isUsed = true;
98
md.update(b, ofs, len);
99
}
100
101
public Object clone() throws CloneNotSupportedException {
102
return this;
103
}
104
}
105
}
106
}
107
108