Class EncryptionUtil

java.lang.Object
ortus.boxlang.runtime.util.EncryptionUtil

public final class EncryptionUtil extends Object
A utility class for encryption and encoding
  • Field Details

    • DEFAULT_HASH_ALGORITHM

      public static final String DEFAULT_HASH_ALGORITHM
      The default algorithm to use
      See Also:
    • DEFAULT_ENCRYPTION_ALGORITHM

      public static final String DEFAULT_ENCRYPTION_ALGORITHM
      Default encryption algorithm
      See Also:
    • DEFAULT_ENCRYPTION_ENCODING

      public static final String DEFAULT_ENCRYPTION_ENCODING
      Default encryption algorithm
      See Also:
    • DEFAULT_ENCRYPTION_KEY_SIZE

      public static final int DEFAULT_ENCRYPTION_KEY_SIZE
      Default key size
      See Also:
    • DEFAULT_ENCODING

      public static final String DEFAULT_ENCODING
      The default encoding to use
      See Also:
    • DEFAULT_ENCRYPTION_ITERATIONS

      public static final int DEFAULT_ENCRYPTION_ITERATIONS
      Default iterations to perform during encryption - the minimum recomended by NIST
      See Also:
    • KEY_ALGORITHMS

      public static final IStruct KEY_ALGORITHMS
      Supported key algorithms key factory algorithms
  • Constructor Details

    • EncryptionUtil

      public EncryptionUtil()
  • Method Details

    • hash

      public static String hash(Object object)
      Performs a hash of an object using the default algorithm
      Parameters:
      object - The object to be hashed
      Returns:
      returns the hashed string
    • hash

      public static String hash(Object object, String algorithm)
      Performs a hash of an object using a supported algorithm
      Parameters:
      object - The object to be hashed
      algorithm - The supported MessageDigest algorithm (case-insensitive)
      Returns:
      returns the hashed string
    • hash

      public static String hash(byte[] byteArray, String algorithm, int iterations)
      Iterative hash of a byte array
      Parameters:
      byteArray -
      algorithm -
      iterations -
      Returns:
    • digestToString

      public static String digestToString(byte[] digest)
      Stringifies a digest
      Parameters:
      digest - The digest
      Returns:
      the strigified result
    • checksum

      public static String checksum(Path filePath)
      Peforms a checksum of a file path object using the MD5 algorithm
      Parameters:
      filePath - The Path object
      Returns:
      returns the checksum string
    • checksum

      public static String checksum(Path filePath, String algorithm)
      Peforms a checksum of a file path object using a supported algorithm
      Parameters:
      filePath - The Path object
      algorithm - The supported MessageDigest algorithm (case-insensitive)
      Returns:
      returns the checksum string
    • hmac

      public static String hmac(byte[] encryptItem, String key, String algorithm, String encoding)
      HMAC encodes a byte array using the default encoding
      Parameters:
      encryptItem - The byte array to encode
      key - The key to use
      algorithm - The algorithm to use
      encoding - The encoding to use
      Returns:
      returns the HMAC encoded string
    • hmac

      public static String hmac(Object input, String key, String algorithm, String encoding)
      HMAC encodes an object using the default encoding
      Parameters:
      input - The object to encode
      key - The key to use
      algorithm - The algorithm to use
      encoding - The encoding to use
      Returns:
      returns the HMAC encoded string
    • base64Encode

      public static String base64Encode(Object item, Charset charset)
      Base64 encodes an object using the default encoding
      Parameters:
      item - The object to encode
      charset - The charset to use
      Returns:
      returns the base64 encoded string
    • urlEncode

      public static String urlEncode(String target)
      URL encodes a string. We use the default encoding
      Parameters:
      target - The string to encode
      Returns:
      returns the URL encoded string
    • urlEncode

      public static String urlEncode(String target, String encoding)
      URL encodes a string. We use the default encoding
      Parameters:
      target - The string to encode
      encoding - The encoding to use
      Returns:
      returns the URL encoded string
    • urlDecode

      public static String urlDecode(String target)
      URL decodes a string We use the default encoding
      Parameters:
      target - The string to decode
      Returns:
      returns the URL decoded string
    • urlDecode

      public static String urlDecode(String target, String encoding)
      URL decodes a string
      Parameters:
      target - The string to decode
      encoding - The encoding to use
      Returns:
      returns the URL decoded string
    • generateKey

      public static SecretKey generateKey(String algorithm, Integer keySize)
      Generates a secret key
      Parameters:
      algorithm - The algorithm to use: AES, ARCFOUR, Blowfish, ChaCha20, DES, DESede, HmacMD5, HmacSHA1, HmacSHA224, HmacSHA256, HmacSHA384, HmacSHA512, HmacSHA3-224, HmacSHA3-256, HmacSHA3-384, HmacSHA3-512
      keySize - The key size
      Returns:
      returns the secret key
    • generateKey

      public static SecretKey generateKey(String algorithm)
      Generate a SecretKey for the given algorithm and an optional key size
      Parameters:
      algorithm - The encryption algorithm
      keySize - The key size
      Returns:
    • encodeKey

      public static String encodeKey(SecretKey key)
      Encode the SecretKey to a string
      Parameters:
      key - The SecretKey to encode
      Returns:
    • decodeKey

      public static SecretKey decodeKey(String key, String algorithm)
      Decodes a secret key from a string representation
      Parameters:
      key - The string representation of the key
      algorithm - The algorithm used to generate the key
      Returns:
    • decodeKeyBytes

      public static byte[] decodeKeyBytes(String key)
      Decode the SecretKey from a string and return the byte array
      Parameters:
      key -
      Returns:
    • generateKeyAsString

      public static String generateKeyAsString(String algorithm, int keySize)
      Generates a secret key using the default algorithm and key size
      Parameters:
      algorithm - The algorithm to use: AES, ARCFOUR, Blowfish, ChaCha20, DES, DESede, HmacMD5, HmacSHA1, HmacSHA224, HmacSHA256, HmacSHA384, HmacSHA512, HmacSHA3-224, HmacSHA3-256, HmacSHA3-384, HmacSHA3-512
      keySize - The key size
      Returns:
      returns the secret key
    • generateKey

      public static SecretKey generateKey()
      Generates a secret key using the default algorithm and key size
    • generateKeyAsString

      public static String generateKeyAsString()
      Generates a secret key using the default algorithm and key size
    • convertSecretKeyToString

      public static String convertSecretKeyToString(SecretKey secretKey)
      Converts a secret key to a string
      Parameters:
      secretKey - The secret key
      Returns:
      returns the secret key as a string
    • createCipher

      public static Cipher createCipher(String algorithm, SecretKey key, AlgorithmParameterSpec params, int cipherMode)
      Creates a cryptographic cipher which can be for encryption and decription
      Parameters:
      algorithm -
      key -
      mode -
      padding -
      Returns:
    • createEncryptionCipher

      public static Cipher createEncryptionCipher(String algorithm, SecretKey key, AlgorithmParameterSpec params)
      Creates a cipher suitable for encryption
      Parameters:
      algorithm - The string representation of the algorithm ( e.g. AES, DES, etc. )
      key - The secret key to use for encryption
      params - The algorithm parameters
      Returns:
    • createDecryptionCipher

      public static Cipher createDecryptionCipher(String algorithm, SecretKey key, AlgorithmParameterSpec params)
      Creates a cipher suitable for decryption
      Parameters:
      algorithm - The string representation of the algorithm ( e.g. AES, DES, etc. )
      key - The secret key to use for decryption
      params - The algorithm parameters
      Returns:
    • encrypt

      public static String encrypt(Object obj, String algorithm, SecretKey key, String encoding, byte[] initVectorOrSalt, Integer iterations)
      Encrypts an object using the specified algorithm and key, with optional vector or salt and iterations
      Parameters:
      obj - The object to encrypt
      algorithm - The string representation of the algorithm ( e.g. AES, DES, etc. )
      key - The secret key to use for encryption
      encoding - The encoding format to return the encrypted object in
      initVectorOrSalt - The initialization vector or salt
      iterations - The number of iterations to use for the algorithm
      Returns:
    • decrypt

      public static Object decrypt(String encrypted, String algorithm, SecretKey key, String encoding, byte[] initVectorOrSalt, Integer iterations)
      Decrypts an object using the specified algorithm and key, with optional vector or salt and iterations
      Parameters:
      encrypted - The encrypted object
      algorithm - The string representation of the algorithm ( e.g. AES, DES, etc. )
      key - The secret key to use for decryption
      encoding - The encoding format of the encrypted object
      initVectorOrSalt - The initialization vector or salt
      iterations - The number of iterations to use for the algorithm
      Returns:
      The decrypted object
    • encodeObject

      public static String encodeObject(byte[] obj, String encoding)
      Encodes an object byte array to the specified string output
      Parameters:
      obj - The object byte array
      encoding - The encoding format to use
      Returns:
      The string representation of the encrypted object
    • decodeString

      public static byte[] decodeString(String encoded, String encoding)
      Decodes an encoded object string to a byte array
      Parameters:
      encoded - The encoded object string
      encoding - The encoding format to use
      Returns:
      The byte array representation of the encrypted object
    • convertToByteArray

      public static byte[] convertToByteArray(Object obj)
      Converts a generic object to a byte array
      Parameters:
      obj -
      Returns: