8286423: Destroy password protection in the example code in KeyStore

Reviewed-by: weijun
This commit is contained in:
Xue-Lei Andrew Fan 2022-05-12 13:51:47 +00:00
parent e4439ca32a
commit 1904e9d280

View file

@ -143,23 +143,24 @@ import sun.security.util.Debug;
* to read existing entries from the keystore, or to write new entries * to read existing entries from the keystore, or to write new entries
* into the keystore: * into the keystore:
* <pre> * <pre>
* KeyStore.ProtectionParameter protParam = * KeyStore.PasswordProtection protParam =
* new KeyStore.PasswordProtection(password); * new KeyStore.PasswordProtection(password);
*
* // get my private key
* KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
* ks.getEntry("privateKeyAlias", protParam);
* PrivateKey myPrivateKey = pkEntry.getPrivateKey();
*
* // save my secret key
* javax.crypto.SecretKey mySecretKey;
* KeyStore.SecretKeyEntry skEntry =
* new KeyStore.SecretKeyEntry(mySecretKey);
* ks.setEntry("secretKeyAlias", skEntry, protParam);
*
* // store away the keystore
* try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) { * try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
* // get my private key
* KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
* ks.getEntry("privateKeyAlias", protParam);
* PrivateKey myPrivateKey = pkEntry.getPrivateKey();
*
* // save my secret key
* javax.crypto.SecretKey mySecretKey;
* KeyStore.SecretKeyEntry skEntry =
* new KeyStore.SecretKeyEntry(mySecretKey);
* ks.setEntry("secretKeyAlias", skEntry, protParam);
*
* // store away the keystore
* ks.store(fos, password); * ks.store(fos, password);
* } finally {
* protParam.destroy();
* } * }
* </pre> * </pre>
* *