Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/crypto/spec/SecretKeySpec.java
41159 views
1
/*
2
* Copyright (c) 1998, 2021, 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 javax.crypto.spec;
27
28
import jdk.internal.access.JavaxCryptoSpecAccess;
29
import jdk.internal.access.SharedSecrets;
30
31
import java.security.MessageDigest;
32
import java.security.spec.KeySpec;
33
import java.util.Arrays;
34
import java.util.Locale;
35
import javax.crypto.SecretKey;
36
37
/**
38
* This class specifies a secret key in a provider-independent fashion.
39
*
40
* <p>It can be used to construct a <code>SecretKey</code> from a byte array,
41
* without having to go through a (provider-based)
42
* <code>SecretKeyFactory</code>.
43
*
44
* <p>This class is only useful for raw secret keys that can be represented as
45
* a byte array and have no key parameters associated with them, e.g., DES or
46
* Triple DES keys.
47
*
48
* @author Jan Luehe
49
*
50
* @see javax.crypto.SecretKey
51
* @see javax.crypto.SecretKeyFactory
52
* @since 1.4
53
*/
54
public class SecretKeySpec implements KeySpec, SecretKey {
55
56
@java.io.Serial
57
private static final long serialVersionUID = 6577238317307289933L;
58
59
/**
60
* The secret key.
61
*
62
* @serial
63
*/
64
private byte[] key;
65
66
/**
67
* The name of the algorithm associated with this key.
68
*
69
* @serial
70
*/
71
private String algorithm;
72
73
static {
74
SharedSecrets.setJavaxCryptoSpecAccess(
75
new JavaxCryptoSpecAccess() {
76
@Override
77
public void clearSecretKeySpec(SecretKeySpec keySpec) {
78
keySpec.clear();
79
}
80
});
81
}
82
83
/**
84
* Constructs a secret key from the given byte array.
85
*
86
* <p>This constructor does not check if the given bytes indeed specify a
87
* secret key of the specified algorithm. For example, if the algorithm is
88
* DES, this constructor does not check if <code>key</code> is 8 bytes
89
* long, and also does not check for weak or semi-weak keys.
90
* In order for those checks to be performed, an algorithm-specific
91
* <i>key specification</i> class (in this case:
92
* {@link DESKeySpec DESKeySpec})
93
* should be used.
94
*
95
* @param key the key material of the secret key. The contents of
96
* the array are copied to protect against subsequent modification.
97
* @param algorithm the name of the secret-key algorithm to be associated
98
* with the given key material.
99
* See the <a href="{@docRoot}/../specs/security/standard-names.html">
100
* Java Security Standard Algorithm Names</a> document
101
* for information about standard algorithm names.
102
* @exception IllegalArgumentException if <code>algorithm</code>
103
* is null or <code>key</code> is null or empty.
104
*/
105
public SecretKeySpec(byte[] key, String algorithm) {
106
if (key == null || algorithm == null) {
107
throw new IllegalArgumentException("Missing argument");
108
}
109
if (key.length == 0) {
110
throw new IllegalArgumentException("Empty key");
111
}
112
this.key = key.clone();
113
this.algorithm = algorithm;
114
}
115
116
/**
117
* Constructs a secret key from the given byte array, using the first
118
* <code>len</code> bytes of <code>key</code>, starting at
119
* <code>offset</code> inclusive.
120
*
121
* <p> The bytes that constitute the secret key are
122
* those between <code>key[offset]</code> and
123
* <code>key[offset+len-1]</code> inclusive.
124
*
125
* <p>This constructor does not check if the given bytes indeed specify a
126
* secret key of the specified algorithm. For example, if the algorithm is
127
* DES, this constructor does not check if <code>key</code> is 8 bytes
128
* long, and also does not check for weak or semi-weak keys.
129
* In order for those checks to be performed, an algorithm-specific key
130
* specification class (in this case:
131
* {@link DESKeySpec DESKeySpec})
132
* must be used.
133
*
134
* @param key the key material of the secret key. The first
135
* <code>len</code> bytes of the array beginning at
136
* <code>offset</code> inclusive are copied to protect
137
* against subsequent modification.
138
* @param offset the offset in <code>key</code> where the key material
139
* starts.
140
* @param len the length of the key material.
141
* @param algorithm the name of the secret-key algorithm to be associated
142
* with the given key material.
143
* See the <a href="{@docRoot}/../specs/security/standard-names.html">
144
* Java Security Standard Algorithm Names</a> document
145
* for information about standard algorithm names.
146
* @exception IllegalArgumentException if <code>algorithm</code>
147
* is null or <code>key</code> is null, empty, or too short,
148
* i.e. {@code key.length-offset<len}.
149
* @exception ArrayIndexOutOfBoundsException is thrown if
150
* <code>offset</code> or <code>len</code> index bytes outside the
151
* <code>key</code>.
152
*/
153
public SecretKeySpec(byte[] key, int offset, int len, String algorithm) {
154
if (key == null || algorithm == null) {
155
throw new IllegalArgumentException("Missing argument");
156
}
157
if (key.length == 0) {
158
throw new IllegalArgumentException("Empty key");
159
}
160
if (key.length-offset < len) {
161
throw new IllegalArgumentException
162
("Invalid offset/length combination");
163
}
164
if (len < 0) {
165
throw new ArrayIndexOutOfBoundsException("len is negative");
166
}
167
this.key = new byte[len];
168
System.arraycopy(key, offset, this.key, 0, len);
169
this.algorithm = algorithm;
170
}
171
172
/**
173
* Returns the name of the algorithm associated with this secret key.
174
*
175
* @return the secret key algorithm.
176
*/
177
public String getAlgorithm() {
178
return this.algorithm;
179
}
180
181
/**
182
* Returns the name of the encoding format for this secret key.
183
*
184
* @return the string "RAW".
185
*/
186
public String getFormat() {
187
return "RAW";
188
}
189
190
/**
191
* Returns the key material of this secret key.
192
*
193
* @return the key material. Returns a new array
194
* each time this method is called.
195
*/
196
public byte[] getEncoded() {
197
return this.key.clone();
198
}
199
200
/**
201
* Calculates a hash code value for the object.
202
* Objects that are equal will also have the same hashcode.
203
*/
204
public int hashCode() {
205
int retval = 0;
206
for (int i = 1; i < this.key.length; i++) {
207
retval += this.key[i] * i;
208
}
209
if (this.algorithm.equalsIgnoreCase("TripleDES"))
210
return (retval ^= "desede".hashCode());
211
else
212
return (retval ^=
213
this.algorithm.toLowerCase(Locale.ENGLISH).hashCode());
214
}
215
216
/**
217
* Tests for equality between the specified object and this
218
* object. Two SecretKeySpec objects are considered equal if
219
* they are both SecretKey instances which have the
220
* same case-insensitive algorithm name and key encoding.
221
*
222
* @param obj the object to test for equality with this object.
223
*
224
* @return true if the objects are considered equal, false if
225
* <code>obj</code> is null or otherwise.
226
*/
227
public boolean equals(Object obj) {
228
if (this == obj)
229
return true;
230
231
if (!(obj instanceof SecretKey))
232
return false;
233
234
String thatAlg = ((SecretKey)obj).getAlgorithm();
235
if (!(thatAlg.equalsIgnoreCase(this.algorithm))) {
236
if ((!(thatAlg.equalsIgnoreCase("DESede"))
237
|| !(this.algorithm.equalsIgnoreCase("TripleDES")))
238
&& (!(thatAlg.equalsIgnoreCase("TripleDES"))
239
|| !(this.algorithm.equalsIgnoreCase("DESede"))))
240
return false;
241
}
242
243
byte[] thatKey = ((SecretKey)obj).getEncoded();
244
try {
245
return MessageDigest.isEqual(this.key, thatKey);
246
} finally {
247
if (thatKey != null) {
248
Arrays.fill(thatKey, (byte)0);
249
}
250
}
251
}
252
253
/**
254
* Clear the key bytes inside.
255
*/
256
void clear() {
257
Arrays.fill(key, (byte)0);
258
}
259
}
260
261