Package org.craftercms.commons.crypto
Class CryptoUtils
- java.lang.Object
-
- org.craftercms.commons.crypto.CryptoUtils
-
public class CryptoUtils extends Object
Utility methods for encryption/decryption and message digest.- Author:
- Alfonso Vásquez
-
-
Field Summary
Fields Modifier and Type Field Description static String
AES_CIPHER_ALGORITHM
static int
AES_KEY_BYTE_SIZE
static String
DEFAULT_AES_CIPHER_TRANSFORMATION
static String
PASSWORD_SEP
static SecureRandom
secureRandom
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
generateAesIv()
Generates a random initialization vector for an AES cipher.static SecretKey
generateAesKey()
Generates a random AES encryption key.static SecretKey
generateKey(String cipherAlgorithm)
Generates a random encryption key.static byte[]
generateRandomBytes(int size)
Generates a random array of bytes, using the singletonSecureRandom
.static String
hashPassword(String clearPswd)
Hashes a password using aSimpleDigest
.static boolean
matchPassword(String hashedPswdAndSalt, String clearPswd)
Returns true if it's a password match, that is, if the hashed clear password equals the given hash.
-
-
-
Field Detail
-
secureRandom
public static final SecureRandom secureRandom
-
AES_CIPHER_ALGORITHM
public static final String AES_CIPHER_ALGORITHM
- See Also:
- Constant Field Values
-
AES_KEY_BYTE_SIZE
public static final int AES_KEY_BYTE_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_AES_CIPHER_TRANSFORMATION
public static final String DEFAULT_AES_CIPHER_TRANSFORMATION
- See Also:
- Constant Field Values
-
PASSWORD_SEP
public static final String PASSWORD_SEP
- See Also:
- Constant Field Values
-
-
Method Detail
-
generateRandomBytes
public static byte[] generateRandomBytes(int size)
Generates a random array of bytes, using the singletonSecureRandom
.- Parameters:
size
- the size of the array- Returns:
- the generated array
-
generateAesKey
public static SecretKey generateAesKey()
Generates a random AES encryption key.- Returns:
- the generated key
-
generateKey
public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException
Generates a random encryption key.- Parameters:
cipherAlgorithm
- the cipher algorithm the key will be used with. Will determine the key size- Returns:
- the generated key
- Throws:
NoSuchAlgorithmException
-
generateAesIv
public static byte[] generateAesIv()
Generates a random initialization vector for an AES cipher.- Returns:
- the generated IV
-
hashPassword
public static String hashPassword(String clearPswd)
Hashes a password using aSimpleDigest
. The generated salt is appended to the password, using thePASSWORD_SEP
.- Parameters:
clearPswd
- the password to hash, in clear- Returns:
- the hashed password +
PASSWORD_SEP
+ salt
-
matchPassword
public static boolean matchPassword(String hashedPswdAndSalt, String clearPswd)
Returns true if it's a password match, that is, if the hashed clear password equals the given hash.- Parameters:
hashedPswdAndSalt
- the hashed password +PASSWORD_SEP
+ salt, as returned byhashPassword(String)
clearPswd
- the password that we're trying to match, in clear- Returns:
- if the password matches
-
-