java.lang.Object
org.seppiko.commons.utils.crypto.CryptoUtil
Crypto util
- Author:
- Leonard Woo
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]cipher(String algorithm, String providerName, int opmode, Key key, AlgorithmParameterSpec params, byte[] data) Cipher utilstatic byte[]cipher(String algorithm, Provider provider, int opmode, Key key, AlgorithmParameterSpec params, byte[] data) Cipher utilstatic ProvidergetProvider(String providerName) Check and get provider objectstatic byte[]keyAgreement(String algorithm, Provider provider, Key privateKey, Key publicKey) get secret keystatic KeyFactorykeyFactory(String algorithm, Provider provider) Returns a KeyFactory object that converts public/private keys of the specified algorithm.static byte[]Mac utilstatic byte[]Mac utilstatic byte[]Message Digest utilstatic byte[]Message Digest utilstatic SecretKeyFactorysecretKeyFactory(String algorithm, Provider provider) Secret Key Factory utilstatic byte[]secretKeyFactory(String algorithm, Provider provider, KeySpec keySpec) Secret Key Factory utilstatic byte[]secureRandom(String algorithm, Provider provider, SecureRandomParameters params, int seedSize) Get secure random bytes
-
Field Details
-
NONPROVIDER
non provider
-
-
Constructor Details
-
CryptoUtil
public CryptoUtil()
-
-
Method Details
-
getProvider
public static Provider getProvider(String providerName) throws IllegalArgumentException, NoSuchProviderException Check and get provider object- Parameters:
providerName- the name of the provider- Returns:
- the provider
- Throws:
IllegalArgumentException- if provider name is null or empty.NoSuchProviderException- if it can not load provider.
-
cipher
public static byte[] cipher(String algorithm, Provider provider, int opmode, Key key, AlgorithmParameterSpec params, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException Cipher util- Parameters:
algorithm- cipher algorithmprovider- cipher provider, if you do not need this setnullorCryptoUtil.NONPROVIDERseeCryptoUtil.getProvider(providerName)opmode- cipher modekey- crypto keyparams- Algorithm Parameter Spec, if you do not need this set nulldata- the input data buffer- Returns:
- the new data buffer with the result
- Throws:
NoSuchPaddingException- if transformation contains a padding scheme that is not available.NoSuchAlgorithmException- if transformation is null, empty, in an invalid format, or if no Provider supports a CipherSpi implementation for the specified algorithm.InvalidAlgorithmParameterException- if the given algorithm parameters are inappropriate for this cipher, or this cipher requires algorithm parameters and params is null, or the given algorithm parameters imply a cryptographic strength that would exceed the legal limits (as determined from the configured jurisdiction policy files).InvalidKeyException- if the given key is inappropriate for initializing this cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).IllegalBlockSizeException- if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.BadPaddingException- if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes.AEADBadTagException- – if this cipher is decrypting in an AEAD mode (such as GCM/CCM), and the received authentication tag does not match the calculated value.- See Also:
-
cipher
public static byte[] cipher(String algorithm, String providerName, int opmode, Key key, AlgorithmParameterSpec params, byte[] data) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException Cipher util- Parameters:
algorithm- cipher algorithmproviderName- cipher provider name, if you do not need this setnullor""opmode- cipher modekey- crypto keyparams- Algorithm Parameter Spec, if you do not need this set nulldata- the input data buffer- Returns:
- the new data buffer with the result
- Throws:
NoSuchPaddingException- if transformation contains a padding scheme that is not available.NoSuchAlgorithmException- if transformation is null, empty, in an invalid format, or if no Provider supports a CipherSpi implementation for the specified algorithm.InvalidAlgorithmParameterException- if the given algorithm parameters are inappropriate for this cipher, or this cipher requires algorithm parameters and params is null, or the given algorithm parameters imply a cryptographic strength that would exceed the legal limits (as determined from the configured jurisdiction policy files).InvalidKeyException- if the given key is inappropriate for initializing this cipher, or its keysize exceeds the maximum allowable keysize (as determined from the configured jurisdiction policy files).IllegalBlockSizeException- if this cipher is a block cipher, no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided.BadPaddingException- if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes.AEADBadTagException- – if this cipher is decrypting in an AEAD mode (such as GCM/CCM), and the received authentication tag does not match the calculated value.- See Also:
-
md
public static byte[] md(String algorithm, byte[] data) throws NoSuchAlgorithmException, NullPointerException Message Digest util- Parameters:
algorithm- Message Digest Algorithmdata- raw data- Returns:
- data hash
- Throws:
NoSuchAlgorithmException- if no Provider supports a MessageDigestSpi implementation for the specified algorithm.NullPointerException- if algorithm is null.- See Also:
-
md
public static byte[] md(String algorithm, Provider provider, byte[] data) throws NoSuchAlgorithmException, NullPointerException, IllegalArgumentException Message Digest util- Parameters:
algorithm- Message Digest Algorithmprovider- the providerdata- raw data- Returns:
- data hash
- Throws:
NoSuchAlgorithmException- if no Provider supports a MessageDigestSpi implementation for the specified algorithm.NullPointerException- if algorithm is null.IllegalArgumentException- if provider is null.- See Also:
-
mac
public static byte[] mac(String algorithm, String keyAlgorithm, byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, NullPointerException Mac util- Parameters:
algorithm- mac hash algorithmkeyAlgorithm- mac hash key algorithmdata- raw datakey- mac hash key- Returns:
- data hash
- Throws:
NoSuchAlgorithmException- if no Provider supports a MacSpi implementation for the specified algorithm.InvalidKeyException- if the given key is inappropriate for initializing this MAC.NullPointerException- if algorithm is null.- See Also:
-
mac
public static byte[] mac(String algorithm, Provider provider, String keyAlgorithm, byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, NullPointerException Mac util- Parameters:
algorithm- mac hash algorithmprovider- the providerkeyAlgorithm- mac hash key algorithmdata- raw datakey- mac hash key- Returns:
- data hash
- Throws:
NoSuchAlgorithmException- if no Provider supports a MacSpi implementation for the specified algorithm.InvalidKeyException- if the given key is inappropriate for initializing this MAC.NullPointerException- if algorithm is null.- See Also:
-
secretKeyFactory
public static byte[] secretKeyFactory(String algorithm, Provider provider, KeySpec keySpec) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalArgumentException, NullPointerException Secret Key Factory util- Parameters:
algorithm- Secret Key Factory algorithmprovider- the providerkeySpec- KeySpec impl e.g.PBEKeySpec- Returns:
- KeySpec data result
- Throws:
NoSuchAlgorithmException- if no Provider supports a SecretKeyFactorySpi implementation for the specified algorithm.InvalidKeySpecException- if the given key specification is inappropriate for this secret-key factory to produce a secret key.IllegalArgumentException- if the provider is null.NullPointerException- if algorithm is null.- See Also:
-
secretKeyFactory
public static SecretKeyFactory secretKeyFactory(String algorithm, Provider provider) throws NoSuchAlgorithmException, IllegalArgumentException, NullPointerException Secret Key Factory util- Parameters:
algorithm- Secret Key Factory algorithmprovider- the provider- Returns:
- KeySpec data result
- Throws:
NoSuchAlgorithmException- if no Provider supports a SecretKeyFactorySpi implementation for the specified algorithm.IllegalArgumentException- if the provider is null.NullPointerException- if algorithm is null.- See Also:
-
keyFactory
public static KeyFactory keyFactory(String algorithm, Provider provider) throws NullPointerException, NoSuchAlgorithmException, IllegalArgumentException Returns a KeyFactory object that converts public/private keys of the specified algorithm.- Parameters:
algorithm- key algorithmprovider- algorithm provider- Returns:
- KeyFactory object
- Throws:
NullPointerException- if algorithm is nullNoSuchAlgorithmException- if a KeyFactorySpi implementation for the specified algorithm is not available from the specified providerIllegalArgumentException- if the specified provider is null
-
keyAgreement
public static byte[] keyAgreement(String algorithm, Provider provider, Key privateKey, Key publicKey) throws NoSuchAlgorithmException, NullPointerException, InvalidKeyException get secret key- Parameters:
algorithm- the standard name of the requested key agreement algorithmprovider- the providerprivateKey- the party's private informationpublicKey- the key for this phase- Returns:
- the new buffer with the shared secret
- Throws:
NoSuchAlgorithmException- if no Provider supports a KeyAgreementSpi implementation for the specified algorithmNullPointerException- if algorithm is nullInvalidKeyException- if the given key is inappropriate for this key agreement or phase
-
secureRandom
public static byte[] secureRandom(String algorithm, Provider provider, SecureRandomParameters params, int seedSize) throws NoSuchAlgorithmException, NullPointerException, IllegalArgumentException Get secure random bytes- Parameters:
algorithm- the name of the RNG algorithm.provider- the provider.params- the SecureRandomParameters the newly created SecureRandom object must support.seedSize- the number of seed bytes to generate.- Returns:
- the seed bytes.
- Throws:
NoSuchAlgorithmException- if no Provider supports the specified algorithm.NullPointerException- if algorithm is null or empty, if the specified provider or params is null.IllegalArgumentException- if seedSize is negative.
-