Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/MacClone.java
41159 views
1
/*
2
* Copyright (c) 1998, 2013, 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 7087021 8013069
27
* @summary Clone tests for all MAC algorithms.
28
* @author Jan Luehe
29
*/
30
import java.security.spec.AlgorithmParameterSpec;
31
import javax.crypto.*;
32
import javax.crypto.spec.*;
33
34
public class MacClone {
35
36
public static void main(String[] args) throws Exception {
37
38
String[] algos = { "HmacMD5", "HmacSHA1", "HmacSHA224", "HmacSHA256",
39
"HmacSHA384", "HmacSHA512" };
40
KeyGenerator kgen = KeyGenerator.getInstance("DES");
41
SecretKey skey = kgen.generateKey();
42
for (String algo : algos) {
43
doTest(algo, skey, null);
44
}
45
46
String[] algos2 = { "HmacPBESHA1", "PBEWithHmacSHA1",
47
"PBEWithHmacSHA224", "PBEWithHmacSHA256",
48
"PBEWithHmacSHA384", "PBEWithHmacSHA512" };
49
skey = new SecretKeySpec("whatever".getBytes(), "PBE");
50
PBEParameterSpec params =
51
new PBEParameterSpec("1234567890".getBytes(), 500);
52
for (String algo : algos2) {
53
doTest(algo, skey, params);
54
}
55
System.out.println("Test Passed");
56
}
57
58
private static void doTest(String algo, SecretKey skey,
59
AlgorithmParameterSpec params) throws Exception {
60
//
61
// Clone an uninitialized Mac object
62
//
63
Mac mac = Mac.getInstance(algo, "SunJCE");
64
Mac macClone = (Mac)mac.clone();
65
System.out.println(macClone.getProvider().toString());
66
System.out.println(macClone.getAlgorithm());
67
boolean thrown = false;
68
try {
69
macClone.update((byte)0x12);
70
} catch (IllegalStateException ise) {
71
thrown = true;
72
}
73
if (!thrown) {
74
throw new Exception("Expected IllegalStateException not thrown");
75
}
76
77
//
78
// Clone an initialized Mac object
79
//
80
mac = Mac.getInstance(algo, "SunJCE");
81
mac.init(skey, params);
82
macClone = (Mac)mac.clone();
83
System.out.println(macClone.getProvider().toString());
84
System.out.println(macClone.getAlgorithm());
85
mac.update((byte)0x12);
86
macClone.update((byte)0x12);
87
byte[] macFinal = mac.doFinal();
88
byte[] macCloneFinal = macClone.doFinal();
89
if (!java.util.Arrays.equals(macFinal, macCloneFinal)) {
90
throw new Exception("ERROR: MAC result of init clone is different");
91
} else System.out.println("MAC check#1 passed");
92
93
//
94
// Clone an updated Mac object
95
//
96
mac.update((byte)0x12);
97
macClone = (Mac)mac.clone();
98
mac.update((byte)0x34);
99
macClone.update((byte)0x34);
100
macFinal = mac.doFinal();
101
macCloneFinal = macClone.doFinal();
102
if (!java.util.Arrays.equals(macFinal, macCloneFinal)) {
103
throw new Exception("ERROR: MAC result of updated clone is different");
104
} else System.out.println("MAC check#2 passed");
105
}
106
}
107
108