Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Mac/HmacPBESHA1.java
41159 views
1
/*
2
* Copyright (c) 2003, 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 4893959 8013069
27
* @summary basic test for PBE MAC algorithms.
28
* @author Valerie Peng
29
*/
30
import java.io.PrintStream;
31
import java.util.*;
32
import java.security.*;
33
import java.security.spec.*;
34
35
import javax.crypto.*;
36
import javax.crypto.spec.*;
37
38
public class HmacPBESHA1 {
39
private static final String[] MAC_ALGOS = {
40
"HmacPBESHA1",
41
"PBEWithHmacSHA1",
42
"PBEWithHmacSHA224",
43
"PBEWithHmacSHA256",
44
"PBEWithHmacSHA384",
45
"PBEWithHmacSHA512"
46
};
47
private static final int[] MAC_LENGTHS = { 20, 20, 28, 32, 48, 64 };
48
private static final String KEY_ALGO = "PBE";
49
private static final String PROVIDER = "SunJCE";
50
51
private static SecretKey key = null;
52
53
public static void main(String argv[]) throws Exception {
54
for (int i = 0; i < MAC_ALGOS.length; i++) {
55
runtest(MAC_ALGOS[i], MAC_LENGTHS[i]);
56
}
57
System.out.println("\nTest Passed");
58
}
59
60
private static void runtest(String algo, int length) throws Exception {
61
System.out.println("Testing: " + algo);
62
if (key == null) {
63
char[] password = { 't', 'e', 's', 't' };
64
PBEKeySpec keySpec = new PBEKeySpec(password);
65
SecretKeyFactory kf =
66
SecretKeyFactory.getInstance(KEY_ALGO, PROVIDER);
67
key = kf.generateSecret(keySpec);
68
}
69
Mac mac = Mac.getInstance(algo, PROVIDER);
70
byte[] plainText = new byte[30];
71
PBEParameterSpec spec =
72
new PBEParameterSpec("saltValue".getBytes(), 250);
73
mac.init(key, spec);
74
mac.update(plainText);
75
byte[] value1 = mac.doFinal();
76
if (value1.length != length) {
77
throw new Exception("incorrect MAC output length, expected " +
78
length + ", got " + value1.length);
79
}
80
mac.update(plainText);
81
byte[] value2 = mac.doFinal();
82
if (!Arrays.equals(value1, value2)) {
83
throw new Exception("generated different MAC outputs");
84
}
85
}
86
}
87
88