Module org.cryptomator.cryptolib
Package org.cryptomator.cryptolib.common
Class DestroyableSecretKey
java.lang.Object
org.cryptomator.cryptolib.common.DestroyableSecretKey
- All Implemented Interfaces:
Serializable,AutoCloseable,Key,SecretKey,Destroyable
- Direct Known Subclasses:
Masterkey
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:
-
Field Summary
Fields inherited from interface javax.crypto.SecretKey
serialVersionUID -
Constructor Summary
ConstructorsConstructorDescriptionDestroyableSecretKey(byte[] key, int offset, int len, String algorithm) Creates a new destroyable secret key, copying of the provided raw key bytes.DestroyableSecretKey(byte[] key, String algorithm) Convenience constructor forDestroyableSecretKey(byte[], int, int, String) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Same asdestroy()copy()Returns an independent copy of this keyvoiddestroy()booleanstatic DestroyableSecretKeyCasts or converts a givenSecretKeyto a DestroyableSecretKeystatic DestroyableSecretKeygenerate(SecureRandom csprng, String algorithm, int keyLenBytes) Creates a new key of given length and for use with given algorithm using entropy from the given csprng.byte[]Returns the raw key bytes this instance wraps.inthashCode()boolean
-
Constructor Details
-
DestroyableSecretKey
Convenience constructor forDestroyableSecretKey(byte[], int, int, String)- Parameters:
key- The raw key data (will get copied)algorithm- Thealgorithm name
-
DestroyableSecretKey
Creates a new destroyable secret key, copying of the provided raw key bytes.- Parameters:
key- A byte[] holding the key material (relevant part will get copied)offset- The offset withinkeywhere the key startslen- The number of bytes beginning atoffsetto read fromkeyalgorithm- Thealgorithm name
-
-
Method Details
-
from
Casts or converts a givenSecretKeyto a DestroyableSecretKey- Parameters:
secretKey- The secret key- Returns:
- Either the provided or a new key, depending on whether the provided key is already a DestroyableSecretKey
-
generate
Creates a new key of given length and for use with given algorithm using entropy from the given csprng.- Parameters:
csprng- A cryptographically secure random number sourcealgorithm- Thekey algorithmkeyLenBytes- The length of the key (in bytes)- Returns:
- A new secret key
-
getAlgorithm
- Specified by:
getAlgorithmin interfaceKey
-
getFormat
-
getEncoded
public byte[] getEncoded()Returns the raw key bytes this instance wraps.Important: Any change to the returned array will reflect in this key. Make sure to
make a local copyif you can't rule out mutations.- Specified by:
getEncodedin interfaceKey- Returns:
- A byte array holding the secret key
-
copy
Returns an independent copy of this key- Returns:
- New copy of
this
-
destroy
public void destroy()- Specified by:
destroyin interfaceDestroyable
-
isDestroyed
public boolean isDestroyed()- Specified by:
isDestroyedin interfaceDestroyable
-
close
-
equals
-
hashCode
-