Package ortus.boxlang.runtime.util
Class EncryptionUtil
java.lang.Object
ortus.boxlang.runtime.util.EncryptionUtil
A utility class for encryption and encoding
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default encoding to usestatic final StringDefault encryption algorithmstatic final StringDefault encryption algorithmstatic final intDefault iterations to perform during encryption - the minimum recomended by NISTstatic final intDefault key sizestatic final StringThe default algorithm to usestatic final IStructSupported key algorithms key factory algorithms -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringbase64Encode(Object item, Charset charset) Base64 encodes an object using the default encodingstatic StringPeforms a checksum of a file path object using the MD5 algorithmstatic StringPeforms a checksum of a file path object using a supported algorithmstatic StringconvertSecretKeyToString(SecretKey secretKey) Converts a secret key to a stringstatic byte[]convertToByteArray(Object obj) Converts a generic object to a byte arraystatic CiphercreateCipher(String algorithm, SecretKey key, AlgorithmParameterSpec params, int cipherMode) Creates a cryptographic cipher which can be for encryption and decriptionstatic CiphercreateDecryptionCipher(String algorithm, SecretKey key, AlgorithmParameterSpec params) Creates a cipher suitable for decryptionstatic CiphercreateEncryptionCipher(String algorithm, SecretKey key, AlgorithmParameterSpec params) Creates a cipher suitable for encryptionstatic SecretKeyDecodes a secret key from a string representationstatic byte[]decodeKeyBytes(String key) Decode the SecretKey from a string and return the byte arraystatic byte[]decodeString(String encoded, String encoding) Decodes an encoded object string to a byte arraystatic Objectdecrypt(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 iterationsstatic StringdigestToString(byte[] digest) Stringifies a digeststatic StringEncode the SecretKey to a stringstatic StringencodeObject(byte[] obj, String encoding) Encodes an object byte array to the specified string outputstatic Stringencrypt(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 iterationsstatic SecretKeyGenerates a secret key using the default algorithm and key sizestatic SecretKeygenerateKey(String algorithm) Generate a SecretKey for the given algorithm and an optional key sizestatic SecretKeygenerateKey(String algorithm, Integer keySize) Generates a secret keystatic StringGenerates a secret key using the default algorithm and key sizestatic StringgenerateKeyAsString(String algorithm, int keySize) Generates a secret key using the default algorithm and key sizestatic StringIterative hash of a byte arraystatic StringPerforms a hash of an object using the default algorithmstatic StringPerforms a hash of an object using a supported algorithmstatic StringHMAC encodes a byte array using the default encodingstatic StringHMAC encodes an object using the default encodingstatic StringURL decodes a string We use the default encodingstatic StringURL decodes a stringstatic StringURL encodes a string.static StringURL encodes a string.
-
Field Details
-
DEFAULT_HASH_ALGORITHM
The default algorithm to use- See Also:
-
DEFAULT_ENCRYPTION_ALGORITHM
Default encryption algorithm- See Also:
-
DEFAULT_ENCRYPTION_ENCODING
Default encryption algorithm- See Also:
-
DEFAULT_ENCRYPTION_KEY_SIZE
public static final int DEFAULT_ENCRYPTION_KEY_SIZEDefault key size- See Also:
-
DEFAULT_ENCODING
The default encoding to use- See Also:
-
DEFAULT_ENCRYPTION_ITERATIONS
public static final int DEFAULT_ENCRYPTION_ITERATIONSDefault iterations to perform during encryption - the minimum recomended by NIST- See Also:
-
KEY_ALGORITHMS
Supported key algorithms key factory algorithms
-
-
Constructor Details
-
EncryptionUtil
public EncryptionUtil()
-
-
Method Details
-
hash
Performs a hash of an object using the default algorithm- Parameters:
object- The object to be hashed- Returns:
- returns the hashed string
-
hash
Performs a hash of an object using a supported algorithm- Parameters:
object- The object to be hashedalgorithm- The supportedMessageDigestalgorithm (case-insensitive)- Returns:
- returns the hashed string
-
hash
Iterative hash of a byte array- Parameters:
byteArray-algorithm-iterations-- Returns:
-
digestToString
Stringifies a digest- Parameters:
digest- The digest- Returns:
- the strigified result
-
checksum
Peforms a checksum of a file path object using the MD5 algorithm- Parameters:
filePath- ThePathobject- Returns:
- returns the checksum string
-
checksum
Peforms a checksum of a file path object using a supported algorithm- Parameters:
filePath- ThePathobjectalgorithm- The supportedMessageDigestalgorithm (case-insensitive)- Returns:
- returns the checksum string
-
hmac
HMAC encodes a byte array using the default encoding- Parameters:
encryptItem- The byte array to encodekey- The key to usealgorithm- The algorithm to useencoding- The encoding to use- Returns:
- returns the HMAC encoded string
-
hmac
HMAC encodes an object using the default encoding- Parameters:
input- The object to encodekey- The key to usealgorithm- The algorithm to useencoding- The encoding to use- Returns:
- returns the HMAC encoded string
-
base64Encode
Base64 encodes an object using the default encoding- Parameters:
item- The object to encodecharset- The charset to use- Returns:
- returns the base64 encoded string
-
urlEncode
URL encodes a string. We use the default encoding- Parameters:
target- The string to encode- Returns:
- returns the URL encoded string
-
urlEncode
URL encodes a string. We use the default encoding- Parameters:
target- The string to encodeencoding- The encoding to use- Returns:
- returns the URL encoded string
-
urlDecode
URL decodes a string We use the default encoding- Parameters:
target- The string to decode- Returns:
- returns the URL decoded string
-
urlDecode
URL decodes a string- Parameters:
target- The string to decodeencoding- The encoding to use- Returns:
- returns the URL decoded string
-
generateKey
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-512keySize- The key size- Returns:
- returns the secret key
-
generateKey
Generate a SecretKey for the given algorithm and an optional key size- Parameters:
algorithm- The encryption algorithmkeySize- The key size- Returns:
-
encodeKey
Encode the SecretKey to a string- Parameters:
key- The SecretKey to encode- Returns:
-
decodeKey
Decodes a secret key from a string representation- Parameters:
key- The string representation of the keyalgorithm- The algorithm used to generate the key- Returns:
-
decodeKeyBytes
Decode the SecretKey from a string and return the byte array- Parameters:
key-- Returns:
-
generateKeyAsString
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-512keySize- The key size- Returns:
- returns the secret key
-
generateKey
Generates a secret key using the default algorithm and key size -
generateKeyAsString
Generates a secret key using the default algorithm and key size -
convertSecretKeyToString
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 encryptionparams- 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 decryptionparams- 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 encryptalgorithm- The string representation of the algorithm ( e.g. AES, DES, etc. )key- The secret key to use for encryptionencoding- The encoding format to return the encrypted object ininitVectorOrSalt- The initialization vector or saltiterations- 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 objectalgorithm- The string representation of the algorithm ( e.g. AES, DES, etc. )key- The secret key to use for decryptionencoding- The encoding format of the encrypted objectinitVectorOrSalt- The initialization vector or saltiterations- The number of iterations to use for the algorithm- Returns:
- The decrypted object
-
encodeObject
Encodes an object byte array to the specified string output- Parameters:
obj- The object byte arrayencoding- The encoding format to use- Returns:
- The string representation of the encrypted object
-
decodeString
Decodes an encoded object string to a byte array- Parameters:
encoded- The encoded object stringencoding- The encoding format to use- Returns:
- The byte array representation of the encrypted object
-
convertToByteArray
Converts a generic object to a byte array- Parameters:
obj-- Returns:
-