Package org.apache.wss4j.common.util
Class KeyUtils
java.lang.Object
org.apache.wss4j.common.util.KeyUtils
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]generateDigest(byte[] inputBytes) Generate a (SHA1) digest of the input bytes.static CiphergetCipherInstance(String cipherAlgo) Translate the "cipherAlgo" URI to a JCE ID, and return a javax.crypto.Cipher instance of this type.static CiphergetCipherInstance(String cipherAlgo, String provider) Translate the "cipherAlgo" URI to a JCE ID, and request a javax.crypto.Cipher instance of this type from the given provider.static KeyGeneratorgetKeyGenerator(String algorithm) static intgetKeyLength(String algorithm) Returns the length of the key in # of bytes.static SecretKeyprepareSecretKey(String algorithm, byte[] rawKey) Convert the raw key bytes into a SecretKey object of type algorithm.static SecretKeyprepareSecretKey(String algorithm, byte[] rawKey, boolean strictKeyLengthCheck) Convert the raw key bytes into a SecretKey object of type algorithm.
-
Field Details
-
RSA_ECB_OAEPWITH_SHA1_AND_MGF1_PADDING
- See Also:
-
-
Method Details
-
getKeyLength
Returns the length of the key in # of bytes. For the HMAC algorithms it guesses a default value that can be used based on the algorithm.- Parameters:
algorithm- the URI of the algorithm. See http://www.w3.org/TR/xmlenc-core1/- Returns:
- the key length
- Throws:
WSSecurityException
-
prepareSecretKey
public static SecretKey prepareSecretKey(String algorithm, byte[] rawKey) throws WSSecurityException Convert the raw key bytes into a SecretKey object of type algorithm. A mismatch between the length of the key and the length required by the algorithm is logged, but not rejected, as the given algorithm is not necessarily the algorithm that the key is going to be used with. This is the case for example when a key is created only to be wrapped in an EncryptedKey structure and handed over to a third party, as done by a STS. UseprepareSecretKey(String, byte[], boolean)with "strictKeyLengthCheck" set to true when the key is about to be used with the given algorithm.- Throws:
WSSecurityException- if the raw key is null or exceeds the maximum allowed size
-
prepareSecretKey
public static SecretKey prepareSecretKey(String algorithm, byte[] rawKey, boolean strictKeyLengthCheck) throws WSSecurityException Convert the raw key bytes into a SecretKey object of type algorithm.- Parameters:
algorithm- the URI of the algorithm the key is associated withrawKey- the raw key bytesstrictKeyLengthCheck- whether to reject a key whose length does not match the length required by the algorithm. This must be set to true when the key is going to be used with the given algorithm, so that key material is never truncated or re-used across algorithms.- Throws:
WSSecurityException- if the raw key is null, exceeds the maximum allowed size, or (if strictKeyLengthCheck is true) does not match the key length required by the algorithm
-
getKeyGenerator
- Throws:
WSSecurityException
-
getCipherInstance
Translate the "cipherAlgo" URI to a JCE ID, and return a javax.crypto.Cipher instance of this type.- Parameters:
cipherAlgo- The cipher in it's WSS URI form, ref. https://www.w3.org/TR/xmlenc-core1/#sec-Algorithms- Throws:
WSSecurityException
-
getCipherInstance
public static Cipher getCipherInstance(String cipherAlgo, String provider) throws WSSecurityException Translate the "cipherAlgo" URI to a JCE ID, and request a javax.crypto.Cipher instance of this type from the given provider.- Parameters:
cipherAlgo- The cipher in it's WSS URI form, ref. https://www.w3.org/TR/xmlenc-core1/#sec-Algorithmsprovider- The provider which shall instantiate the cipher.- Throws:
WSSecurityException
-
generateDigest
Generate a (SHA1) digest of the input bytes. The MessageDigest instance that backs this method is cached for efficiency.- Parameters:
inputBytes- the bytes to digest- Returns:
- the digest of the input bytes
- Throws:
WSSecurityException
-