Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/PrivateKeyEqualityTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 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
import java.security.KeyFactory;
24
import java.security.KeyPairGenerator;
25
import java.security.KeyPair;
26
import java.security.NoSuchAlgorithmException;
27
import java.security.NoSuchProviderException;
28
import java.security.interfaces.RSAPrivateCrtKey;
29
import java.security.interfaces.RSAPrivateKey;
30
import java.security.spec.InvalidKeySpecException;
31
import java.security.spec.RSAPrivateKeySpec;
32
import java.security.spec.PKCS8EncodedKeySpec;
33
import java.security.spec.RSAPrivateCrtKeySpec;
34
35
/**
36
* @test
37
* @bug 8044199 4666485
38
* @summary Equality checking for RSAPrivateKey by SunRsaSign provider.
39
*/
40
public class PrivateKeyEqualityTest {
41
/**
42
* ALGORITHM name, fixed as RSA.
43
*/
44
private static final String KEYALG = "RSA";
45
46
/**
47
* JDK default RSA Provider.
48
*/
49
private static final String PROVIDER_NAME = "SunRsaSign";
50
51
public static void main(String[] args) throws NoSuchAlgorithmException,
52
NoSuchProviderException, InvalidKeySpecException {
53
// Generate the first key.
54
KeyPairGenerator generator
55
= KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
56
KeyPair keyPair = generator.generateKeyPair();
57
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
58
if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
59
System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
60
throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
61
}
62
63
// Generate the second key.
64
KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
65
RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(
66
rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
67
RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(
68
rsaPrivateKeySpec);
69
70
// Generate the third key.
71
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(
72
rsaPrivateKey.getEncoded());
73
RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(
74
encodedKeySpec);
75
76
// Check for equality.
77
if (rsaPrivateKey.equals(rsaPrivateKey2)) {
78
throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
79
}
80
if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
81
throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
82
}
83
if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
84
throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
85
}
86
if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
87
throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
88
}
89
90
// Generate the fourth key.
91
RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey)rsaPrivateKey;
92
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(
93
rsaPrivateCrtKey.getModulus(),
94
rsaPrivateCrtKey.getPublicExponent(),
95
rsaPrivateCrtKey.getPrivateExponent(),
96
rsaPrivateCrtKey.getPrimeP(),
97
rsaPrivateCrtKey.getPrimeQ(),
98
rsaPrivateCrtKey.getPrimeExponentP(),
99
rsaPrivateCrtKey.getPrimeExponentQ(),
100
rsaPrivateCrtKey.getCrtCoefficient()
101
);
102
RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(
103
rsaPrivateCrtKeySpec);
104
if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
105
throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
106
}
107
}
108
}
109
110