public class CryptoUtils extends Object
Modifier and Type | Field and 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 |
Modifier and Type | Method and 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 singleton
SecureRandom . |
static String |
hashPassword(String clearPswd)
Hashes a password using a
SimpleDigest . |
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.
|
public static final SecureRandom secureRandom
public static final String AES_CIPHER_ALGORITHM
public static final int AES_KEY_BYTE_SIZE
public static final String DEFAULT_AES_CIPHER_TRANSFORMATION
public static final String PASSWORD_SEP
public static byte[] generateRandomBytes(int size)
SecureRandom
.size
- the size of the arraypublic static SecretKey generateAesKey()
public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException
cipherAlgorithm
- the cipher algorithm the key will be used with. Will determine the key sizeNoSuchAlgorithmException
public static byte[] generateAesIv()
public static String hashPassword(String clearPswd)
SimpleDigest
. The generated salt is appended
to the password, using the PASSWORD_SEP
.clearPswd
- the password to hash, in clearPASSWORD_SEP
+ saltpublic static boolean matchPassword(String hashedPswdAndSalt, String clearPswd)
hashedPswdAndSalt
- the hashed password + PASSWORD_SEP
+ salt, as returned by
hashPassword(String)
clearPswd
- the password that we're trying to match, in clearCopyright © 2023 CrafterCMS. All rights reserved.