26
26
package com .sun .crypto .provider ;
27
27
28
28
import java .io .ObjectStreamException ;
29
+ import java .lang .ref .Reference ;
29
30
import java .lang .ref .Cleaner ;
30
31
import java .nio .ByteBuffer ;
31
32
import java .nio .CharBuffer ;
@@ -205,7 +206,12 @@ public boolean equals(Object obj) {
205
206
}
206
207
207
208
public byte [] getEncoded () {
208
- return key .clone ();
209
+ try {
210
+ return key .clone ();
211
+ } finally {
212
+ // prevent this from being cleaned for the above block
213
+ Reference .reachabilityFence (this );
214
+ }
209
215
}
210
216
211
217
public String getAlgorithm () {
@@ -221,7 +227,12 @@ public void clear() {
221
227
}
222
228
223
229
public char [] getPassword () {
224
- return passwd .clone ();
230
+ try {
231
+ return passwd .clone ();
232
+ } finally {
233
+ // prevent this from being cleaned for the above block
234
+ Reference .reachabilityFence (this );
235
+ }
225
236
}
226
237
227
238
public byte [] getSalt () {
@@ -237,30 +248,45 @@ public String getFormat() {
237
248
* Objects that are equal will also have the same hashcode.
238
249
*/
239
250
public int hashCode () {
240
- int retval = 0 ;
241
- for (int i = 1 ; i < this .key .length ; i ++) {
242
- retval += this .key [i ] * i ;
251
+ try {
252
+ int retval = 0 ;
253
+ for (int i = 1 ; i < this .key .length ; i ++) {
254
+ retval += this .key [i ] * i ;
255
+ }
256
+ return (retval ^= getAlgorithm ().toLowerCase
257
+ (Locale .ENGLISH ).hashCode ());
258
+ } finally {
259
+ // prevent this from being cleaned for the above block
260
+ Reference .reachabilityFence (this );
243
261
}
244
- return (retval ^= getAlgorithm ().toLowerCase (Locale .ENGLISH ).hashCode ());
245
262
}
246
263
247
264
public boolean equals (Object obj ) {
248
- if (obj == this )
249
- return true ;
265
+ try {
266
+ if (obj == this ) {
267
+ return true ;
268
+ }
250
269
251
- if (!(obj instanceof SecretKey ))
252
- return false ;
270
+ if (!(obj instanceof SecretKey )) {
271
+ return false ;
272
+ }
253
273
254
- SecretKey that = (SecretKey ) obj ;
274
+ SecretKey that = (SecretKey ) obj ;
255
275
256
- if (!(that .getAlgorithm ().equalsIgnoreCase (getAlgorithm ())))
257
- return false ;
258
- if (!(that .getFormat ().equalsIgnoreCase ("RAW" )))
259
- return false ;
260
- byte [] thatEncoded = that .getEncoded ();
261
- boolean ret = MessageDigest .isEqual (key , thatEncoded );
262
- Arrays .fill (thatEncoded , (byte )0x00 );
263
- return ret ;
276
+ if (!(that .getAlgorithm ().equalsIgnoreCase (getAlgorithm ()))) {
277
+ return false ;
278
+ }
279
+ if (!(that .getFormat ().equalsIgnoreCase ("RAW" ))) {
280
+ return false ;
281
+ }
282
+ byte [] thatEncoded = that .getEncoded ();
283
+ boolean ret = MessageDigest .isEqual (key , thatEncoded );
284
+ Arrays .fill (thatEncoded , (byte )0x00 );
285
+ return ret ;
286
+ } finally {
287
+ // prevent this from being cleaned for the above block
288
+ Reference .reachabilityFence (this );
289
+ }
264
290
}
265
291
266
292
/**
@@ -273,7 +299,12 @@ public boolean equals(Object obj) {
273
299
*/
274
300
@ java .io .Serial
275
301
private Object writeReplace () throws ObjectStreamException {
276
- return new KeyRep (KeyRep .Type .SECRET , getAlgorithm (),
277
- getFormat (), key );
302
+ try {
303
+ return new KeyRep (KeyRep .Type .SECRET , getAlgorithm (),
304
+ getFormat (), key );
305
+ } finally {
306
+ // prevent this from being cleaned for the above block
307
+ Reference .reachabilityFence (this );
308
+ }
278
309
}
279
310
}
0 commit comments