Package org.wildfly.security.key
Class KeyUtil
- java.lang.Object
-
- org.wildfly.security.key.KeyUtil
-
public final class KeyUtil extends Object
Key utility methods.- Author:
- David M. Lloyd
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classKeyUtil.KeyClonerCreator
-
Field Summary
Fields Modifier and Type Field Description private static KeyUtil.KeyClonerCreatorCLONER_CREATORCache so that we only have to figure out a cloning strategy for a given class one time.
-
Constructor Summary
Constructors Modifier Constructor Description privateKeyUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Key>
TcloneKey(Class<T> expectType, T key)Attempt to create a safe clone of the given key object.static AlgorithmParameterSpecgetParameters(Key key)Attempt to acquire parameters from the given key.static <P extends AlgorithmParameterSpec>
PgetParameters(Key key, Class<P> paramSpecClass)Attempt to acquire parameters of the given type from the given key.static booleanhasParameters(Key key, AlgorithmParameterSpec parameters)Determine if the given key has parameters which match the given parameters.static booleanhasSameParameters(Key key1, Key key2)Attempt to determine if the two keys have the same parameters.static booleanparametersEqual(AlgorithmParameterSpec p1, AlgorithmParameterSpec p2)Attempt to determine if two algorithm parameter specifications are equal.static intparametersHashCode(AlgorithmParameterSpec param)Attempt to get a stable hash code for the given parameter specification.
-
-
-
Field Detail
-
CLONER_CREATOR
private static final KeyUtil.KeyClonerCreator CLONER_CREATOR
Cache so that we only have to figure out a cloning strategy for a given class one time.
-
-
Method Detail
-
getParameters
public static AlgorithmParameterSpec getParameters(Key key)
Attempt to acquire parameters from the given key.- Parameters:
key- the key (must not benull)- Returns:
- the parameters, or
nullif no known parameters are available
-
getParameters
public static <P extends AlgorithmParameterSpec> P getParameters(Key key, Class<P> paramSpecClass)
Attempt to acquire parameters of the given type from the given key.- Type Parameters:
P- the parameter specification type- Parameters:
key- the key (must not benull)paramSpecClass- the parameter specification class (must not benull)- Returns:
- the parameters, or
nullif no known parameters of the given type are available
-
hasParameters
public static boolean hasParameters(Key key, AlgorithmParameterSpec parameters)
Determine if the given key has parameters which match the given parameters.- Parameters:
key- the key (must not benull)parameters- the parameters (must not benull)- Returns:
trueif the parameters match,falseotherwise
-
parametersEqual
public static boolean parametersEqual(AlgorithmParameterSpec p1, AlgorithmParameterSpec p2)
Attempt to determine if two algorithm parameter specifications are equal. This method will returntrueif the parameters are definitely the same, orfalseif they are not definitely equal or equivalency cannot be determined.- Parameters:
p1- the first parameter specification (must not benull)p2- the second parameter specification (must not benull)- Returns:
trueif the parameters are definitely equal,falseotherwise
-
parametersHashCode
public static int parametersHashCode(AlgorithmParameterSpec param)
Attempt to get a stable hash code for the given parameter specification. If a stable hash code cannot be acquired, the hash code of the class is returned, which results in correct (if non-optimal) behavior. If the parameter isnull, a hash code of zero is returned.- Parameters:
param- the parameter specification- Returns:
- the hash code
-
hasSameParameters
public static boolean hasSameParameters(Key key1, Key key2)
Attempt to determine if the two keys have the same parameters. This method returnstrueif the keys definitely have the same parameters, orfalseif they do not or if parameter equivalency cannot be determined.- Parameters:
key1- the first key (must not benull)key2- the second key (must not benull)- Returns:
trueif the parameters are definitely equal,falseotherwise
-
cloneKey
public static <T extends Key> T cloneKey(Class<T> expectType, T key)
Attempt to create a safe clone of the given key object. This algorithm first checks to see if the key's class implementsDestroyable; if not, it is returned as-is. Next it checks to see if the key has been destroyed; if so, it is returned as-is. Next it determines if the key actually implements theDestroyableinterface; if not, it is returned as-is. Then it determines if there is a publicclonemethod that returns a compatible type; if so, that method is used. Then it determines if the key implements a known key interface; if so, a raw implementation of that interface is produced. Last it checks to see if the key is some other unknownSecretKeytype; if so, it captures its value using aSecretKeySpec. If none of these checks succeed, an exception is thrown.- Parameters:
expectType- the expected result type (must not benull)key- the key object- Returns:
- the cloned key, or the original if the key type is not destroyable
-
-