- All Implemented Interfaces:
AutoCloseable
,Cloneable
,Destroyable
Key
, which goes out of its way to protect the key, an unprotected key provides access
to the key value. This is intended for when the key needs to be accessible to the application, such as
setting a cookie value to a generated authentication token.- Author:
- AO Industries, Inc.
-
Constructor Summary
ConstructorDescriptionUnprotectedKey
(byte[] key) UnprotectedKey
(int keyBytes) Generates a new key of the given number of bytes.UnprotectedKey
(int keyBytes, SecureRandom secureRandom) Generates a new key of the given number of bytes using the providedSecureRandom
source.UnprotectedKey
(int keyBytes, Random random) Deprecated.UnprotectedKey
(SupplierE<? extends byte[], Ex> generator) Generates a new key using the provided key generator. -
Method Summary
Modifier and TypeMethodDescription<Ex extends Throwable>
voidCalls a consumer, providing a copy of the key.clone()
int
hashCode()
Implements hash code viaArrays.hashCode(byte[])
on the key.<R,
Ex extends Throwable>
RCalls a function, providing a copy of the key.<Ex extends Throwable>
booleantest
(PredicateE<? super byte[], Ex> predicate) Calls a predicate, providing a copy of the key.
-
Constructor Details
-
UnprotectedKey
- Parameters:
key
- Is zeroed before this method returns. If the original key is needed, pass a copy to this method.- Throws:
IllegalArgumentException
- whenkey == null || key.length == 0
or whenkey
is already destroyed (contains all zeroes).
-
UnprotectedKey
Generates a new key using the provided key generator.The key will never be all-zeroes, since this would conflict with the representation of already destroyed. In the unlikely event the generator creates an all-zero key, the key will be discarded and another will be generated. We do recognize that disallowing certain values from the key space may provide an advantage to attackers (i.e. Enigma), losing the all-zero key is probably a good choice anyway.
- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
Ex
-
UnprotectedKey
Deprecated.Please useSecureRandom
. This method will stay, but will remain deprecated since it should only be used after careful consideration.Generates a new key of the given number of bytes using the providedRandom
source.The key will never be all-zeroes, since this would conflict with the representation of already destroyed. In the unlikely event the random source generates an all-zero key, the key will be discarded and another will be generated. We do recognize that disallowing certain values from the key space may provide an advantage to attackers (i.e. Enigma), losing the all-zero key is probably a good choice anyway.
- Throws:
IllegalArgumentException
- whenkeyBytes == 0
-
UnprotectedKey
Generates a new key of the given number of bytes using the providedSecureRandom
source.The key will never be all-zeroes, since this would conflict with the representation of already destroyed. In the unlikely event the random source generates an all-zero key, the key will be discarded and another will be generated. We do recognize that disallowing certain values from the key space may provide an advantage to attackers (i.e. Enigma), losing the all-zero key is probably a good choice anyway.
- Throws:
IllegalArgumentException
- whenkeyBytes == 0
-
UnprotectedKey
Generates a new key of the given number of bytes.The key will never be all-zeroes, since this would conflict with the representation of already destroyed. In the unlikely event the random source generates an all-zero key, the key will be discarded and another will be generated. We do recognize that disallowing certain values from the key space may provide an advantage to attackers (i.e. Enigma), losing the all-zero key is probably a good choice anyway.
- Throws:
IllegalArgumentException
- whenkeyBytes == 0
-
-
Method Details
-
hashCode
public int hashCode()Implements hash code viaArrays.hashCode(byte[])
on the key. Please note that the hash code may change when destroyed. -
clone
-
invoke
public <R,Ex extends Throwable> R invoke(FunctionE<? super byte[], R, throws IllegalStateException, ExEx> function) Calls a function, providing a copy of the key. The copy of the key is zeroed once the function returns.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
IllegalStateException
- whenKey.isDestroyed()
Ex
-
accept
public <Ex extends Throwable> void accept(ConsumerE<? super byte[], Ex> consumer) throws IllegalStateException, ExCalls a consumer, providing a copy of the key. The copy of the key is zeroed once the consumer returns.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
IllegalStateException
- whenKey.isDestroyed()
Ex
-
test
public <Ex extends Throwable> boolean test(PredicateE<? super byte[], Ex> predicate) throws IllegalStateException, ExCalls a predicate, providing a copy of the key. The copy of the key is zeroed once the predicate returns.- Type Parameters:
Ex
- An arbitrary exception type that may be thrown- Throws:
IllegalStateException
- whenKey.isDestroyed()
Ex
-
SecureRandom
.