java.lang.Object
org.seppiko.commons.utils.CryptoUtil
Crypto util
https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html
- Author:
- Leonard Woo
-
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 KeyPairkeyPairGenerator(String algorithm, int keysize)Asymmetric cryptography key pair generatorstatic byte[]Mac utilstatic byte[]Message Digest utilstatic byte[]secretKeyFactory(String algorithm, KeySpec keySpec)Secret Key Factory utilstatic byte[]signatureSign(String algorithm, PrivateKey privateKey, byte[] rawData)Data Signature Encryptstatic booleansignatureVerify(String algorithm, PublicKey publicKey, byte[] rawData, byte[] signedData)Data Signature Verification
-
Field Details
-
NONPROVIDER
non provider
-
-
Constructor Details
-
CryptoUtil
public CryptoUtil()
-
-
Method Details
-
cipher
public static byte[] cipher(String algorithm, Provider provider, int opmode, Key key, AlgorithmParameterSpec params, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingExceptionCipher util- Parameters:
algorithm- cipher algorithmprovider- cipher provider, if you do not need this setnullorCryptoUtil.NONPROVIDERseeSecurity.getProvider(providerName)opmode- cipher modekey- crypto keyparams- Algorithm Parameter Spec, if you do not need this set nulldata- raw data- Returns:
- data
- 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
-
cipher
public static byte[] cipher(String algorithm, String providerName, int opmode, Key key, AlgorithmParameterSpec params, byte[] data) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyExceptionCipher 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- raw data- Returns:
- data
- 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
-
md
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.- See Also:
MessageDigest
-
mac
public static byte[] mac(byte[] data, byte[] salt, String algorithm, String keyAlgorithm) throws NoSuchAlgorithmException, InvalidKeyExceptionMac util- Parameters:
data- raw datasalt- mac hash saltalgorithm- mac hash algorithmkeyAlgorithm- mac hash key algorithm- 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.- See Also:
Mac
-
secretKeyFactory
public static byte[] secretKeyFactory(String algorithm, KeySpec keySpec) throws NoSuchAlgorithmException, InvalidKeySpecExceptionSecret Key Factory util- Parameters:
algorithm- Secret Key Factory algorithmkeySpec- 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.- See Also:
SecretKeyFactory
-
keyPairGenerator
public static KeyPair keyPairGenerator(String algorithm, int keysize) throws NoSuchAlgorithmExceptionAsymmetric cryptography key pair generator- Parameters:
algorithm- Asymmetric cryptography key algorithmkeysize- key size- Returns:
- key pair
- Throws:
NoSuchAlgorithmException- if no Provider supports a KeyPairGeneratorSpi implementation for the specified algorithm.- See Also:
KeyPairGenerator
-
signatureSign
public static byte[] signatureSign(String algorithm, PrivateKey privateKey, byte[] rawData) throws NoSuchAlgorithmException, InvalidKeyException, SignatureExceptionData Signature Encrypt- Parameters:
algorithm- signature algorithmprivateKey- signature private keyrawData- data- Returns:
- signed data
- Throws:
NoSuchAlgorithmException- if no Provider supports a Signature implementation for the specified algorithmInvalidKeyException- if the key is invalidSignatureException- if this signature object is not initialized properly- See Also:
Signature
-
signatureVerify
public static boolean signatureVerify(String algorithm, PublicKey publicKey, byte[] rawData, byte[] signedData) throws NoSuchAlgorithmException, InvalidKeyException, SignatureExceptionData Signature Verification- Parameters:
algorithm- signature algorithmpublicKey- signature private keyrawData- datasignedData- signed data- Returns:
- true is verified
- Throws:
NoSuchAlgorithmException- if no Provider supports a Signature implementation for the specified algorithmInvalidKeyException- if the key is invalidSignatureException- if this signature object is not initialized properly- See Also:
Signature
-