Package africa.absa.inception.core.util
Class CryptoUtil
- java.lang.Object
-
- africa.absa.inception.core.util.CryptoUtil
-
public class CryptoUtil extends Object
The CryptoUtil class provides a number of cryptography related utility functions.- Author:
- Marcus Portmann
-
-
Field Summary
Fields Modifier and Type Field Description static intAES_BLOCK_SIZEThe AES block size.static intAES_KEY_SIZEThe AES key size.static StringAES_KEY_SPECThe AES key specification.static StringAES_TRANSFORMATION_NAMEThe AES tranformation name.
-
Constructor Summary
Constructors Constructor Description CryptoUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]createRandomEncryptionIV(int length)Creates a random encryption initialization vector with the specified length.static KeyPairgetKeyPair(KeyStore keyStore, String alias, String password)Retrieve the key pair with the specified alias from the key store.static byte[]getRandomAESKey()Returns a randomly generated AES key.static KeyStoreloadKeyStore(String type, String path, String password)Load a key store.static KeyStoreloadKeyStore(String type, String path, String password, String alias)Load a key store and query it to confirm a key pair with the specified alias is present.static KeyStoreloadTrustStore(String type, String path, String password)Load the trust store.static byte[]passwordTo3DESKey(String password)Convert the specified password to a 3DES key that can be used with the 3DES cypher encrypt and decrypt functions.static byte[]passwordToAESKey(String password)Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.static byte[]passwordToAESKey(String password, byte[] salt)Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.static byte[]passwordToAESKey(String password, String salt)Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.
-
-
-
Field Detail
-
AES_BLOCK_SIZE
public static final int AES_BLOCK_SIZE
The AES block size.- See Also:
- Constant Field Values
-
AES_KEY_SIZE
public static final int AES_KEY_SIZE
The AES key size.- See Also:
- Constant Field Values
-
AES_KEY_SPEC
public static final String AES_KEY_SPEC
The AES key specification.- See Also:
- Constant Field Values
-
AES_TRANSFORMATION_NAME
public static final String AES_TRANSFORMATION_NAME
The AES tranformation name.- See Also:
- Constant Field Values
-
-
Method Detail
-
createRandomEncryptionIV
public static byte[] createRandomEncryptionIV(int length)
Creates a random encryption initialization vector with the specified length.- Parameters:
length- the length of the random encryption initialization vector- Returns:
- the random encryption initialization vector
-
getKeyPair
public static KeyPair getKeyPair(KeyStore keyStore, String alias, String password) throws GeneralSecurityException
Retrieve the key pair with the specified alias from the key store.- Parameters:
keyStore- the key storealias- the alias for the key pairpassword- the password for the key- Returns:
- the key pair
- Throws:
GeneralSecurityException- if the key pair could not be retrieved
-
getRandomAESKey
public static byte[] getRandomAESKey()
Returns a randomly generated AES key.- Returns:
- a randomly generated AES key
-
loadKeyStore
public static KeyStore loadKeyStore(String type, String path, String password) throws GeneralSecurityException
Load a key store.- Parameters:
type- the key store type e.g. JKS, PKCS12, etcpath- the path to the key storepassword- the key store password- Returns:
- the key store that was loaded
- Throws:
GeneralSecurityException- if the key store could not be loaded
-
loadKeyStore
public static KeyStore loadKeyStore(String type, String path, String password, String alias) throws GeneralSecurityException
Load a key store and query it to confirm a key pair with the specified alias is present.- Parameters:
type- the key store type e.g. JKS, PKCS12, etcpath- the path to the key storepassword- the key store passwordalias- the alias for the key pair in the key store that should be retrieved- Returns:
- the key store that was loaded
- Throws:
GeneralSecurityException- if the key store could not be loaded
-
loadTrustStore
public static KeyStore loadTrustStore(String type, String path, String password) throws GeneralSecurityException
Load the trust store.- Parameters:
type- the trust store type e.g. JKS, PKCS12, etcpath- the path to the trust storepassword- the trust store password- Returns:
- the trust store that was loaded
- Throws:
GeneralSecurityException- if the trust store could not be loaded
-
passwordTo3DESKey
public static byte[] passwordTo3DESKey(String password) throws CryptoException
Convert the specified password to a 3DES key that can be used with the 3DES cypher encrypt and decrypt functions.- Parameters:
password- the password to convert to a 3DES key- Returns:
- the 3DES key
- Throws:
CryptoException
-
passwordToAESKey
public static byte[] passwordToAESKey(String password) throws CryptoException
Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.- Parameters:
password- the password to convert to an AES key- Returns:
- the AES key
- Throws:
CryptoException
-
passwordToAESKey
public static byte[] passwordToAESKey(String password, byte[] salt) throws CryptoException
Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.- Parameters:
password- the password to convert to an AES keysalt- the salt to use when generating the AES key- Returns:
- the AES key
- Throws:
CryptoException
-
passwordToAESKey
public static byte[] passwordToAESKey(String password, String salt) throws CryptoException
Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.- Parameters:
password- the password to convert to an AES keysalt- the salt to use when generating the AES key- Returns:
- the AES key
- Throws:
CryptoException
-
-