Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/javax/crypto/spec/SecretKeySpec.java
67743 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 (offset < 0) {
161
throw new ArrayIndexOutOfBoundsException("offset is negative");
162
}
163
if (len < 0) {
164
throw new ArrayIndexOutOfBoundsException("len is negative");
165
}
166
if (key.length - offset < len) {
167
throw new IllegalArgumentException
168
("Invalid offset/length combination");
169
}
170
this.key = new byte[len];
171
System.arraycopy(key, offset, this.key, 0, len);
172
this.algorithm = algorithm;
173
}
174
175
/**
176
* Returns the name of the algorithm associated with this secret key.
177
*
178
* @return the secret key algorithm.
179
*/
180
public String getAlgorithm() {
181
return this.algorithm;
182
}
183
184
/**
185
* Returns the name of the encoding format for this secret key.
186
*
187
* @return the string "RAW".
188
*/
189
public String getFormat() {
190
return "RAW";
191
}
192
193
/**
194
* Returns the key material of this secret key.
195
*
196
* @return the key material. Returns a new array
197
* each time this method is called.
198
*/
199
public byte[] getEncoded() {
200
return this.key.clone();
201
}
202
203
/**
204
* Calculates a hash code value for the object.
205
* Objects that are equal will also have the same hashcode.
206
*/
207
public int hashCode() {
208
int retval = 0;
209
for (int i = 1; i < this.key.length; i++) {
210
retval += this.key[i] * i;
211
}
212
if (this.algorithm.equalsIgnoreCase("TripleDES"))
213
return (retval ^= "desede".hashCode());
214
else
215
return (retval ^=
216
this.algorithm.toLowerCase(Locale.ENGLISH).hashCode());
217
}
218
219
/**
220
* Tests for equality between the specified object and this
221
* object. Two SecretKeySpec objects are considered equal if
222
* they are both SecretKey instances which have the
223
* same case-insensitive algorithm name and key encoding.
224
*
225
* @param obj the object to test for equality with this object.
226
*
227
* @return true if the objects are considered equal, false if
228
* <code>obj</code> is null or otherwise.
229
*/
230
public boolean equals(Object obj) {
231
if (this == obj)
232
return true;
233
234
if (!(obj instanceof SecretKey))
235
return false;
236
237
String thatAlg = ((SecretKey)obj).getAlgorithm();
238
if (!(thatAlg.equalsIgnoreCase(this.algorithm))) {
239
if ((!(thatAlg.equalsIgnoreCase("DESede"))
240
|| !(this.algorithm.equalsIgnoreCase("TripleDES")))
241
&& (!(thatAlg.equalsIgnoreCase("TripleDES"))
242
|| !(this.algorithm.equalsIgnoreCase("DESede"))))
243
return false;
244
}
245
246
byte[] thatKey = ((SecretKey)obj).getEncoded();
247
try {
248
return MessageDigest.isEqual(this.key, thatKey);
249
} finally {
250
if (thatKey != null) {
251
Arrays.fill(thatKey, (byte)0);
252
}
253
}
254
}
255
256
/**
257
* Clear the key bytes inside.
258
*/
259
void clear() {
260
Arrays.fill(key, (byte)0);
261
}
262
}
263
264