2626package com .sun .crypto .provider ;
2727
2828import java .io .ObjectStreamException ;
29+ import java .lang .ref .Reference ;
2930import java .lang .ref .Cleaner ;
3031import java .nio .ByteBuffer ;
3132import java .nio .CharBuffer ;
@@ -205,7 +206,12 @@ public boolean equals(Object obj) {
205206 }
206207
207208 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+ }
209215 }
210216
211217 public String getAlgorithm () {
@@ -221,7 +227,12 @@ public void clear() {
221227 }
222228
223229 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+ }
225236 }
226237
227238 public byte [] getSalt () {
@@ -237,30 +248,45 @@ public String getFormat() {
237248 * Objects that are equal will also have the same hashcode.
238249 */
239250 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 );
243261 }
244- return (retval ^= getAlgorithm ().toLowerCase (Locale .ENGLISH ).hashCode ());
245262 }
246263
247264 public boolean equals (Object obj ) {
248- if (obj == this )
249- return true ;
265+ try {
266+ if (obj == this ) {
267+ return true ;
268+ }
250269
251- if (!(obj instanceof SecretKey ))
252- return false ;
270+ if (!(obj instanceof SecretKey )) {
271+ return false ;
272+ }
253273
254- SecretKey that = (SecretKey ) obj ;
274+ SecretKey that = (SecretKey ) obj ;
255275
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+ }
264290 }
265291
266292 /**
@@ -273,7 +299,12 @@ public boolean equals(Object obj) {
273299 */
274300 @ java .io .Serial
275301 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+ }
278309 }
279310}
0 commit comments