java-topology/test/jdk/com/sun/crypto/provider/KeyFactory/PBEKeyDestroyTest.java
russell@unturf.com 0a580b313d undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
2026-03-26 17:11:57 -04:00

75 lines
2.7 KiB
Java

/*
* Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8312306
* @summary Check the destroy()/isDestroyed() of the PBEKey impl from SunJCE
* @library /test/lib
* @run junit/othervm PBEKeyDestroyTest
*/
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class PBEKeyDestroyTest {
@Test
public void test() throws Exception {
PBEKeySpec keySpec = new PBEKeySpec("12345678".toCharArray(),
"abcdefgh".getBytes(StandardCharsets.UTF_8), 100000, 128 >> 3);
SecretKeyFactory skf = SecretKeyFactory.getInstance
("PBEWithHmacSHA1AndAES_128", "SunJCE");
SecretKey key1 = skf.generateSecret(keySpec);
SecretKey key2 = skf.generateSecret(keySpec);
// should be equal
assertFalse(key1.isDestroyed());
assertFalse(key2.isDestroyed());
assertEquals(key1, key2);
assertEquals(key2, key1);
// destroy key1
key1.destroy();
assertTrue(key1.isDestroyed());
assertNotEquals(key1, key2);
assertNotEquals(key2, key1);
// also destroy key2
key2.destroy();
assertTrue(key2.isDestroyed());
assertNotEquals(key1, key2);
assertNotEquals(key2, key1);
// call destroy again to make sure no unexpected exceptions
key2.destroy();
}
}