Class DestroyableSecretKey

java.lang.Object
org.cryptomator.cryptolib.common.DestroyableSecretKey
All Implemented Interfaces:
Serializable, AutoCloseable, Key, SecretKey, Destroyable
Direct Known Subclasses:
Masterkey

public class DestroyableSecretKey extends Object implements SecretKey, AutoCloseable
A SecretKey that (other than JDK's SecretKeySpec) actually implements Destroyable.

Furthermore, this implementation will not create copies when accessing getEncoded(). Instead it implements copy() and AutoCloseable in an exception-free manner. To prevent mutation of the exposed key, you would want to make sure to always work on scoped copies, such as in this example:

     // copy "key" to protect it from unwanted modifications:
     try (DestroyableSecretKey k = key.copy()) {
         // use "k":
         Cipher cipher = Cipher.init(k, ...)
         cipher.doFinal(...)
     } // "k" will get destroyed here
 
See Also: