Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/spec/PKCS8EncodedKeySpec/Algorithm.java
41155 views
1
/*
2
* Copyright (c) 2014, 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 8047223
27
* @summary Add algorithm parameter to PKCS8EncodedKeySpec class
28
*/
29
import java.security.spec.PKCS8EncodedKeySpec;
30
import java.util.Base64;
31
import javax.crypto.EncryptedPrivateKeyInfo;
32
import javax.crypto.SecretKey;
33
import javax.crypto.SecretKeyFactory;
34
import javax.crypto.spec.PBEKeySpec;
35
36
public class Algorithm {
37
38
private static String PKCS8PrivateKey =
39
"MIICoTAbBgkqhkiG9w0BBQMwDgQIqQMPwbNEhOgCAggABIICgCwRkeLXVGdO7S1h\n" +
40
"FAFUiwj1HCzqYFF2x9+FzjlXNwEWecZsor5eoKQlTtJ9dsPajQ/wFgY76lkXDQXE\n" +
41
"hdm8ndWFgCwqFBshmAp4TOvO9GlaAloDTnLMUg715D5FujiElcV7vqIY2V/7uB21\n" +
42
"YRanKUa21sZAFJGj6Hom1+5+k0Q7Xi4kHgt+ZIPNLwrNFPWVovbTJdScZuJaDp6m\n" +
43
"Q1DJUIQOzthV11VI+MU/v5SSKhj/uCaxizazEi5lgdmR7rRGgMz2YipOIjXIsKgu\n" +
44
"jKX5LYFAZ8nYq1hy8Q1JPR5VPuWMFqeyofO/teXJb8gI/4TC1ZoED8hXj07jpJqG\n" +
45
"2NVO1Dwqab31qSAjfjBkSYHKun63BvZPq2mT+frJF1YzvQhCDnWN1zbMKFNTZJfd\n" +
46
"cUaecH/fgNKwKpeKGgX7UlWxo26/lS8pBiJ5ihtbyFfMUBtlwEN5uOHqVFOeZp1Z\n" +
47
"DwCc0o1JA7yOcazA2TtNT9pc58tFZ8pEeyLj7ZchOgv06N0hZJsI6AiwII4ljd+K\n" +
48
"4WKvs/xiSZU3tcHaWzqlf+6/M5kC3Pihm9GhZbKBmvrZYiKyTlJEeVI3pFRNSqbE\n" +
49
"nZUJgkmgzNT/ZfM2WsUJm03Rq0eNCU/FDscIZnCWSA6Bf/DJDQWmhMhg2QmTGzQM\n" +
50
"hw/vy77q7jxV67s36HGxxR1oe8uoZ2zugBBxHWEdqyQyrVwZXJukdjrc2S7pvMln\n" +
51
"/VSleEf91MEcDhztyhPSqlX+H95vMnVmh5oY2gwY+P0oD5Eki6/9K+BHfuqgtS4S\n" +
52
"LIna1iSyLr17pRO1lmNtvuCMwmUjeI8w3JhLmxxx//bl/WCAekqj3nMplrJHZ7xd\n" +
53
"6k0Stxo=";
54
55
private static String keyAlg = "RSA";
56
private static String password = "password";
57
58
/*
59
* This test checks that a PKCS8EncodedKeySpec is properly constructed
60
* from an encrypted private key and that the key algorithm name can be
61
* retrieved as expected.
62
*/
63
public static void main(String[] argv) throws Exception {
64
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
65
Base64.getMimeDecoder().decode(PKCS8PrivateKey));
66
PBEKeySpec pks = new PBEKeySpec(password.toCharArray());
67
SecretKeyFactory skf = SecretKeyFactory.getInstance(epki.getAlgName());
68
SecretKey sk = skf.generateSecret(pks);
69
PKCS8EncodedKeySpec keySpec = epki.getKeySpec(sk);
70
71
// Get the key algorithm and make sure it's what we expect
72
String alg = keySpec.getAlgorithm();
73
if (!alg.equals(keyAlg)) {
74
throw new Exception("Expected: " + keyAlg + ", Got: " + alg);
75
}
76
77
System.out.println("Test passed");
78
}
79
}
80
81