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 Details

  • Method Details

    • 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
    • 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 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