Package org.seppiko.commons.utils
Class CryptoUtil
java.lang.Object
org.seppiko.commons.utils.CryptoUtil
public class CryptoUtil
extends java.lang.Object
Crypto util
https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html
- Author:
- Leonard Woo
-
Constructor Summary
Constructors Constructor Description CryptoUtil() -
Method Summary
Modifier and Type Method Description static byte[]cipher(java.lang.String algorithm, java.lang.String keyAlgorithm, byte[] data, byte[] key, int opmode)Cipher utilstatic byte[]cipher(java.lang.String algorithm, java.lang.String keyAlgorithm, byte[] data, byte[] key, int opmode, java.security.spec.AlgorithmParameterSpec params)Cipher utilstatic java.security.KeyPairkeyPairGenerator(java.lang.String algorithm, int keysize)Asymmetric cryptography key pair generatorstatic byte[]mac(byte[] data, byte[] salt, java.lang.String algorithm, java.lang.String keyAlgorithm)Mac utilstatic byte[]md(java.lang.String algorithm, byte[] data)Message Digest utilstatic byte[]secretKeyFactory(java.lang.String algorithm, java.security.spec.KeySpec keySpec)Secret Key Factory utilstatic byte[]signatureSign(java.lang.String algorithm, java.security.PrivateKey privateKey, byte[] rawData)Data Signaturestatic booleansignatureVerify(java.lang.String algorithm, java.security.PublicKey publicKey, byte[] rawData, byte[] signedData)Data Signature VerificationMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
CryptoUtil
public CryptoUtil()
-
-
Method Details
-
cipher
public static byte[] cipher(java.lang.String algorithm, java.lang.String keyAlgorithm, byte[] data, byte[] key, int opmode, java.security.spec.AlgorithmParameterSpec params) throws javax.crypto.NoSuchPaddingException, java.security.NoSuchAlgorithmException, java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingExceptionCipher util- Parameters:
algorithm- Cipher algorithmkeyAlgorithm- Secret key algorithmdata- raw datakey- crypto keyopmode- cipher modeparams- Algorithm Parameter Spec- Returns:
- data
- Throws:
javax.crypto.NoSuchPaddingException- if transformation contains a padding scheme that is not available.java.security.NoSuchAlgorithmException- if transformation is null, empty, in an invalid format, or if no Provider supports a CipherSpi implementation for the specified algorithm.java.security.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).java.security.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).javax.crypto.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.javax.crypto.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.javax.crypto.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(java.lang.String algorithm, java.lang.String keyAlgorithm, byte[] data, byte[] key, int opmode) throws javax.crypto.NoSuchPaddingException, java.security.NoSuchAlgorithmException, java.security.InvalidAlgorithmParameterException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingExceptionCipher util- Parameters:
algorithm- Cipher algorithmkeyAlgorithm- Secret key algorithmdata- raw datakey- crypto keyopmode- cipher mode- Returns:
- data
- Throws:
javax.crypto.NoSuchPaddingException- if transformation contains a padding scheme that is not available.java.security.NoSuchAlgorithmException- if transformation is null, empty, in an invalid format, or if no Provider supports a CipherSpi implementation for the specified algorithm.java.security.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).java.security.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).javax.crypto.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.javax.crypto.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.javax.crypto.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
public static byte[] md(java.lang.String algorithm, byte[] data) throws java.security.NoSuchAlgorithmExceptionMessage Digest util- Parameters:
algorithm- Message Digest Algorithmdata- raw data- Returns:
- data hash
- Throws:
java.security.NoSuchAlgorithmException- if no Provider supports a MessageDigestSpi implementation for the specified algorithm.- See Also:
MessageDigest
-
mac
public static byte[] mac(byte[] data, byte[] salt, java.lang.String algorithm, java.lang.String keyAlgorithm) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyExceptionMac util- Parameters:
data- raw datasalt- mac hash saltalgorithm- mac hash algorithmkeyAlgorithm- mac hash key algorithm- Returns:
- data hash
- Throws:
java.security.NoSuchAlgorithmException- if no Provider supports a MacSpi implementation for the specified algorithm.java.security.InvalidKeyException- if the given key is inappropriate for initializing this MAC.- See Also:
Mac
-
secretKeyFactory
public static byte[] secretKeyFactory(java.lang.String algorithm, java.security.spec.KeySpec keySpec) throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecExceptionSecret Key Factory util- Parameters:
algorithm- Secret Key Factory algorithmkeySpec- KeySpec impl e.g.PBEKeySpec- Returns:
- KeySpec data result
- Throws:
java.security.NoSuchAlgorithmException- if no Provider supports a SecretKeyFactorySpi implementation for the specified algorithm.java.security.spec.InvalidKeySpecException- if the given key specification is inappropriate for this secret-key factory to produce a secret key.- See Also:
SecretKeyFactory
-
keyPairGenerator
public static java.security.KeyPair keyPairGenerator(java.lang.String algorithm, int keysize) throws java.security.NoSuchAlgorithmExceptionAsymmetric cryptography key pair generator- Parameters:
algorithm- Asymmetric cryptography key algorithmkeysize- key size- Returns:
- key pair
- Throws:
java.security.NoSuchAlgorithmException- if no Provider supports a KeyPairGeneratorSpi implementation for the specified algorithm.
-
signatureSign
public static byte[] signatureSign(java.lang.String algorithm, java.security.PrivateKey privateKey, byte[] rawData) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, java.security.SignatureExceptionData Signature- Parameters:
algorithm- signature algorithmprivateKey- signature private keyrawData- data- Returns:
- signed data
- Throws:
java.security.NoSuchAlgorithmException- if no Provider supports a Signature implementation for the specified algorithmjava.security.InvalidKeyException- if the key is invalidjava.security.SignatureException- if this signature object is not initialized properly
-
signatureVerify
public static boolean signatureVerify(java.lang.String algorithm, java.security.PublicKey publicKey, byte[] rawData, byte[] signedData) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, java.security.SignatureExceptionData Signature Verification- Parameters:
algorithm- signature algorithmpublicKey- signature private keyrawData- datasignedData- signed data- Returns:
- true is verified
- Throws:
java.security.NoSuchAlgorithmException- if no Provider supports a Signature implementation for the specified algorithmjava.security.InvalidKeyException- if the key is invalidjava.security.SignatureException- if this signature object is not initialized properly
-