Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/DSA/TestAlgParameterGenerator.java
41154 views
1
/*
2
* Copyright (c) 2012, 2017, 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 7044060 8055351 8181048
27
* @summary verify that DSA parameter generation works
28
* @run main/timeout=600 TestAlgParameterGenerator
29
*/
30
31
import java.security.AlgorithmParameterGenerator;
32
import java.security.AlgorithmParameters;
33
import java.security.spec.DSAGenParameterSpec;
34
import java.security.spec.DSAParameterSpec;
35
36
public class TestAlgParameterGenerator {
37
38
private static void checkParamStrength(AlgorithmParameters param,
39
int strength) throws Exception {
40
String algo = param.getAlgorithm();
41
if (!algo.equalsIgnoreCase("DSA")) {
42
throw new RuntimeException("Unexpected type of parameters: " + algo);
43
}
44
DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
45
int valueL = spec.getP().bitLength();
46
if (strength != valueL) {
47
System.out.println("Expected " + strength + " but actual " + valueL);
48
throw new RuntimeException("Wrong P strength");
49
}
50
}
51
52
private static void checkParamStrength(AlgorithmParameters param,
53
DSAGenParameterSpec genParam)
54
throws Exception {
55
String algo = param.getAlgorithm();
56
if (!algo.equalsIgnoreCase("DSA")) {
57
throw new RuntimeException("Unexpected type of parameters: " + algo);
58
}
59
DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
60
int valueL = spec.getP().bitLength();
61
int strength = genParam.getPrimePLength();
62
if (strength != valueL) {
63
System.out.println("P: Expected " + strength + " but actual " + valueL);
64
throw new RuntimeException("Wrong P strength");
65
}
66
int valueN = spec.getQ().bitLength();
67
strength = genParam.getSubprimeQLength();
68
if (strength != valueN) {
69
System.out.println("Q: Expected " + strength + " but actual " + valueN);
70
throw new RuntimeException("Wrong Q strength");
71
}
72
}
73
74
public static void main(String[] args) throws Exception {
75
AlgorithmParameterGenerator apg
76
= AlgorithmParameterGenerator.getInstance("DSA", "SUN");
77
long start, stop;
78
79
// make sure no-init still works
80
start = System.currentTimeMillis();
81
AlgorithmParameters param = apg.generateParameters();
82
stop = System.currentTimeMillis();
83
System.out.println("Time: " + (stop - start) + " ms.");
84
85
// make sure the old model works
86
int[] strengths = {512, 768, 1024};
87
for (int sizeP : strengths) {
88
System.out.println("Generating " + sizeP + "-bit DSA Parameters");
89
start = System.currentTimeMillis();
90
apg.init(sizeP);
91
param = apg.generateParameters();
92
stop = System.currentTimeMillis();
93
System.out.println("Time: " + (stop - start) + " ms.");
94
checkParamStrength(param, sizeP);
95
}
96
97
// now the newer model
98
DSAGenParameterSpec[] specSet = {
99
new DSAGenParameterSpec(1024, 160),
100
new DSAGenParameterSpec(2048, 224),
101
new DSAGenParameterSpec(2048, 256)
102
// no support for prime size 3072
103
// ,new DSAGenParameterSpec(3072, 256)
104
};
105
106
for (DSAGenParameterSpec genParam : specSet) {
107
System.out.println("Generating (" + genParam.getPrimePLength()
108
+ ", " + genParam.getSubprimeQLength() + ") DSA Parameters");
109
start = System.currentTimeMillis();
110
apg.init(genParam, null);
111
param = apg.generateParameters();
112
stop = System.currentTimeMillis();
113
System.out.println("Time: " + (stop - start) + " ms.");
114
checkParamStrength(param, genParam);
115
}
116
}
117
}
118
119