Class CryptoUtils


  • public class CryptoUtils
    extends Object
    Utility methods for encryption/decryption and message digest.
    Author:
    Alfonso Vásquez
    • Method Detail

      • generateRandomBytes

        public static byte[] generateRandomBytes​(int size)
        Generates a random array of bytes, using the singleton SecureRandom.
        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
      • 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 a SimpleDigest. The generated salt is appended to the password, using the PASSWORD_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 by hashPassword(String)
        clearPswd - the password that we're trying to match, in clear
        Returns:
        if the password matches