Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ParametersMap.java
41161 views
1
/*
2
* Copyright (c) 2020, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.ec;
27
28
import sun.security.util.ObjectIdentifier;
29
import sun.security.x509.AlgorithmId;
30
31
import java.security.spec.AlgorithmParameterSpec;
32
import java.security.spec.NamedParameterSpec;
33
import java.util.Collections;
34
import java.util.Map;
35
import java.util.HashMap;
36
import java.util.Optional;
37
import java.util.function.Function;
38
import java.util.function.Supplier;
39
40
public class ParametersMap<T> {
41
42
private Map<Integer, T> sizeMap = new HashMap<Integer, T>();
43
private Map<ObjectIdentifier, T> oidMap =
44
new HashMap<ObjectIdentifier, T>();
45
private Map<String, T> nameMap = new HashMap<String, T>();
46
47
48
public void fix() {
49
50
sizeMap = Collections.unmodifiableMap(sizeMap);
51
oidMap = Collections.unmodifiableMap(oidMap);
52
nameMap = Collections.unmodifiableMap(nameMap);
53
}
54
55
public void put(String name, ObjectIdentifier oid, int size, T params) {
56
nameMap.put(name.toLowerCase(), params);
57
oidMap.put(oid, params);
58
sizeMap.put(size, params);
59
}
60
61
public Optional<T> getByOid(ObjectIdentifier id) {
62
return Optional.ofNullable(oidMap.get(id));
63
}
64
public Optional<T> getBySize(int size) {
65
return Optional.ofNullable(sizeMap.get(size));
66
}
67
public Optional<T> getByName(String name) {
68
return Optional.ofNullable(nameMap.get(name.toLowerCase()));
69
}
70
71
// Utility method that is used by the methods below to handle exception
72
// suppliers
73
private static
74
<A, B> Supplier<B> apply(final Function<A, B> func, final A a) {
75
return new Supplier<B>() {
76
@Override
77
public B get() {
78
return func.apply(a);
79
}
80
};
81
}
82
83
/**
84
* Get parameters by key size, or throw an exception if no parameters are
85
* defined for the specified key size. This method is used in several
86
* contexts that should throw different exceptions when the parameters
87
* are not found. The first argument is a function that produces the
88
* desired exception.
89
*
90
* @param exception a function that produces an exception from a string
91
* @param size the desired key size
92
* @param <E> the type of exception that is thrown
93
* @return the parameters for the specified key size
94
* @throws T when suitable parameters do not exist
95
*/
96
public
97
<E extends Throwable>
98
T getBySize(Function<String, E> exception,
99
int size) throws E {
100
101
Optional<T> paramsOpt = getBySize(size);
102
return paramsOpt.orElseThrow(
103
apply(exception, "Unsupported size: " + size));
104
}
105
106
/**
107
* Get parameters by algorithm ID, or throw an exception if no
108
* parameters are defined for the specified ID. This method is used in
109
* several contexts that should throw different exceptions when the
110
* parameters are not found. The first argument is a function that produces
111
* the desired exception.
112
*
113
* @param exception a function that produces an exception from a string
114
* @param algId the algorithm ID
115
* @param <E> the type of exception that is thrown
116
* @return the parameters for the specified algorithm ID
117
* @throws E when suitable parameters do not exist
118
*/
119
public
120
<E extends Throwable>
121
T get(Function<String, E> exception,
122
AlgorithmId algId) throws E {
123
124
Optional<T> paramsOpt = getByOid(algId.getOID());
125
return paramsOpt.orElseThrow(
126
apply(exception, "Unsupported OID: " + algId.getOID()));
127
}
128
129
/**
130
* Get parameters by algorithm parameter spec, or throw an exception if no
131
* parameters are defined for the spec. This method is used in
132
* several contexts that should throw different exceptions when the
133
* parameters are not found. The first argument is a function that produces
134
* the desired exception.
135
*
136
* @param exception a function that produces an exception from a string
137
* @param params the algorithm parameters spec
138
* @param <E> the type of exception that is thrown
139
* @return the parameters for the spec
140
* @throws E when suitable parameters do not exist
141
*/
142
public
143
<E extends Throwable>
144
T get(Function<String, E> exception,
145
AlgorithmParameterSpec params) throws E {
146
147
if (params instanceof NamedParameterSpec) {
148
NamedParameterSpec namedParams = (NamedParameterSpec) params;
149
Optional<T> paramsOpt = getByName(namedParams.getName());
150
return paramsOpt.orElseThrow(
151
apply(exception, "Unsupported name: " + namedParams.getName()));
152
} else {
153
throw exception.apply("Only NamedParameterSpec is supported.");
154
}
155
}
156
}
157
158