Cryptography helper providing hashing, HMAC-style key derivation, symmetric encryption and Diffie-Hellman key exchange for templates and scripts. Registered as the crypto property on the platform's template Formatter, so app developers reach it as formatter.crypto from Velocity templates and GraalJS. Methods accept byte arrays, base64 strings or plain strings interchangeably and delegate the actual cipher, digest and key agreement work to the Crypto utility class.

Group: Managers


Methods

hmacHashAsByteArray(Object key, Object message, Integer iterations, Integer length, String algorithm) · hmacHash(Object key, Object message, Integer iterations, Integer length, String algorithm) · hmacHashAsByteArray(String digest, Object key, Object message) · hmacHashAsByteArray(String digest, Object key, Object message, Integer length) · hmacHash(String digest, Object key, Object message) · hmacHash(String digest, Object key, Object message, Integer length) · decryptAsByteArray(String transformation, Object encryptedVal, Object key, Object iv) · decrypt(String transformation, Object encryptedVal, Object key, Object iv) · decryptAsByteArray(String transformation, Object encryptedVal, Object key) · decrypt(String transformation, Object encryptedVal, Object key) · encryptAsByteArray(String transformation, Object val, Object key, Object iv) · encrypt(String transformation, Object val, Object key, Object iv) · encryptAsByteArray(String transformation, Object val, Object key) · encrypt(String transformation, Object val, Object key) · createDiffieHellman() · createDiffieHellman(String prime, String prime_encoding) · createHash(String algorithm) · createMac(String algorithm, String key, String message) · createMacAsByteArray(String algorithm, String key, String message) · hmacEquals(byte[] digestA, byte[] digestB) · generateRandomAsBytes(Integer size) · generateCurveKeyPair(String curve, String algorithm) · generateCurveKeyPairAsBytes(String curve, String algorithm) · createSecretKeyFromBytes(String algorithm, byte[] secretKeyBytes) · createSecretKeyFromBase64String(String algorithm, String base64EncodedSecretKey)

hmacHashAsByteArray(Object key, Object message, Integer iterations, Integer length, String algorithm)

Returns: byte[]

Derives a key from the given password and salt using a PBKDF2-style SecretKeyFactory algorithm, and returns the raw derived key bytes. Intended for password-based key derivation rather than for hashing arbitrary HMAC digests, despite the method name.

ParameterDescription
keythe password material; accepts a byte array, a char array or a String
messagethe salt; accepts a byte array or a String
iterationsthe number of PBKDF2 iterations to apply
lengththe desired key length in bytes; internally converted to bits for the key spec
algorithmthe SecretKeyFactory algorithm name, for example PBKDF2WithHmacSHA1; see the JDK's list of supported Mac algorithm names

hmacHash(Object key, Object message, Integer iterations, Integer length, String algorithm)

Returns: String

Derives a key from the given password and salt using a PBKDF2-style SecretKeyFactory algorithm, the same as hmacHashAsByteArray, and returns it as a base64-encoded string.

ParameterDescription
keythe password material; accepts a byte array, a char array or a String
messagethe salt; accepts a byte array or a String
iterationsthe number of PBKDF2 iterations to apply
lengththe desired key length in bytes; internally converted to bits for the key spec
algorithmthe SecretKeyFactory algorithm name, for example PBKDF2WithHmacSHA1

hmacHashAsByteArray(String digest, Object key, Object message)

Returns: byte[]

Derives a 64-byte key from the given password and salt using PBKDF2WithHmacSHA1 with 1000 iterations, and returns the raw derived key bytes. The digest parameter is currently not used by the derivation.

ParameterDescription
digestnot used by the current implementation
keythe password material; accepts a byte array or a String
messagethe salt; accepts a byte array or a String

hmacHashAsByteArray(String digest, Object key, Object message, Integer length)

Returns: byte[]

Derives a key from the given password and salt using PBKDF2WithHmacSHA1 with 1000 iterations, and returns the raw derived key bytes. The digest parameter is currently not used; the algorithm is always PBKDF2WithHmacSHA1 regardless of what is passed in.

ParameterDescription
digestnot used by the current implementation
keythe password material; accepts a byte array or a String
messagethe salt; accepts a byte array or a String
lengththe desired key length in bytes; defaults to 64 when null

hmacHash(String digest, Object key, Object message)

Returns: String

Derives a 64-byte key from the given password and salt using PBKDF2WithHmacSHA1, the same as hmacHashAsByteArray, and returns it as a base64-encoded string.

ParameterDescription
digestnot used by the current implementation
keythe password material; accepts a byte array or a String
messagethe salt; accepts a byte array or a String

hmacHash(String digest, Object key, Object message, Integer length)

Returns: String

Derives a key from the given password and salt using PBKDF2WithHmacSHA1, the same as hmacHashAsByteArray, and returns it as a base64-encoded string.

ParameterDescription
digestnot used by the current implementation
keythe password material; accepts a byte array or a String
messagethe salt; accepts a byte array or a String
lengththe desired key length in bytes; defaults to 64 when null

decryptAsByteArray(String transformation, Object encryptedVal, Object key, Object iv)

Returns: byte[]

Decrypts an AES value using the given cipher transformation and initialisation vector, and returns the raw decrypted bytes.

ParameterDescription
transformationthe cipher transformation name, for example AES/CBC/PKCS5Padding
encryptedValthe ciphertext to decrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String
ivthe initialisation vector; accepts a byte array or a String

decrypt(String transformation, Object encryptedVal, Object key, Object iv)

Returns: String

Decrypts an AES value using the given cipher transformation and initialisation vector, the same as decryptAsByteArray, and returns the plaintext as a String built from the raw decrypted bytes.

ParameterDescription
transformationthe cipher transformation name, for example AES/CBC/PKCS5Padding
encryptedValthe ciphertext to decrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String
ivthe initialisation vector; accepts a byte array or a String

decryptAsByteArray(String transformation, Object encryptedVal, Object key)

Returns: byte[]

Decrypts an AES value using the given cipher transformation with no initialisation vector, and returns the raw decrypted bytes. Suited to transformations that do not require an IV, such as ECB mode.

ParameterDescription
transformationthe cipher transformation name, for example AES/ECB/PKCS5Padding
encryptedValthe ciphertext to decrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String

decrypt(String transformation, Object encryptedVal, Object key)

Returns: String

Decrypts an AES value using the given cipher transformation with no initialisation vector, the same as decryptAsByteArray, and returns the plaintext as a String built from the raw decrypted bytes.

ParameterDescription
transformationthe cipher transformation name, for example AES/ECB/PKCS5Padding
encryptedValthe ciphertext to decrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String

encryptAsByteArray(String transformation, Object val, Object key, Object iv)

Returns: byte[]

Encrypts a value as AES using the given cipher transformation and initialisation vector, and returns the raw ciphertext bytes.

ParameterDescription
transformationthe cipher transformation name, for example AES/CBC/PKCS5Padding
valthe plaintext to encrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String
ivthe initialisation vector; accepts a byte array or a String

encrypt(String transformation, Object val, Object key, Object iv)

Returns: String

Encrypts a value as AES using the given cipher transformation and initialisation vector, the same as encryptAsByteArray, and returns the ciphertext as a base64-encoded string.

ParameterDescription
transformationthe cipher transformation name, for example AES/CBC/PKCS5Padding
valthe plaintext to encrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String
ivthe initialisation vector; accepts a byte array or a String

encryptAsByteArray(String transformation, Object val, Object key)

Returns: byte[]

Encrypts a value as AES using the given cipher transformation with no initialisation vector, and returns the raw ciphertext bytes. Suited to transformations that do not require an IV, such as ECB mode.

ParameterDescription
transformationthe cipher transformation name, for example AES/ECB/PKCS5Padding
valthe plaintext to encrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String

encrypt(String transformation, Object val, Object key)

Returns: String

Encrypts a value as AES using the given cipher transformation with no initialisation vector, the same as encryptAsByteArray, and returns the ciphertext as a base64-encoded string.

ParameterDescription
transformationthe cipher transformation name, for example AES/ECB/PKCS5Padding
valthe plaintext to encrypt; accepts a byte array or a String
keythe AES key; accepts a byte array or a String

createDiffieHellman()

Returns: DiffieHellmanModule

Creates a Diffie-Hellman key exchange helper using a randomly generated prime.

createDiffieHellman(String prime, String prime_encoding)

Returns: DiffieHellmanModule

Creates a Diffie-Hellman key exchange helper using the given prime.

ParameterDescription
primethe prime to use, encoded according to prime_encoding
prime_encodingthe encoding of the prime parameter; base64 is decoded, any other value is treated as raw bytes

createHash(String algorithm)

Returns: MessageDigest

Creates a MessageDigest instance for the given algorithm, for example SHA-256, ready to accumulate data and compute a hash.

ParameterDescription
algorithmthe digest algorithm name; case-insensitive

createMac(String algorithm, String key, String message)

Returns: String

Computes an HMAC of the given message using the given algorithm and secret key, and returns it as a hexadecimal string.

ParameterDescription
algorithmthe Mac algorithm name, for example HmacSHA256
keythe secret key, encoded as UTF-8 bytes
messagethe message to authenticate, encoded as UTF-8 bytes

createMacAsByteArray(String algorithm, String key, String message)

Returns: byte[]

Computes an HMAC of the given message using the given algorithm and secret key, and returns the raw digest bytes.

ParameterDescription
algorithmthe Mac algorithm name, for example HmacSHA256
keythe secret key, encoded as UTF-8 bytes
messagethe message to authenticate, encoded as UTF-8 bytes

hmacEquals(byte[] digestA, byte[] digestB)

Returns: boolean

Compares two digests for equality using a constant-time comparison, to avoid leaking timing information that could help an attacker guess a correct digest byte by byte.

ParameterDescription
digestAthe first digest to compare
digestBthe second digest to compare

generateRandomAsBytes(Integer size)

Returns: byte[]

Generates cryptographically random bytes using a SHA1PRNG secure random source.

ParameterDescription
sizethe number of random bytes to generate

generateCurveKeyPair(String curve, String algorithm)

Returns: KeyPair

Generates an elliptic-curve key pair for the named curve using the Bouncy Castle provider.

ParameterDescription
curvethe name of the named EC curve, as registered with Bouncy Castle's curve table
algorithmthe key pair algorithm name, for example EC or ECDSA

generateCurveKeyPairAsBytes(String curve, String algorithm)

Returns: Map<String,byte[]>

Generates an elliptic-curve key pair for the named curve, the same as generateCurveKeyPair, and returns the raw encoded key material in a map keyed by privateKey and publicKey.

ParameterDescription
curvethe name of the named EC curve, as registered with Bouncy Castle's curve table
algorithmthe key pair algorithm name, for example EC or ECDSA

createSecretKeyFromBytes(String algorithm, byte[] secretKeyBytes)

Returns: SecretKey

Wraps raw key bytes as a SecretKey for the given algorithm, without copying or validating the bytes.

ParameterDescription
algorithmthe key algorithm name, for example AES
secretKeyBytesthe raw key bytes

createSecretKeyFromBase64String(String algorithm, String base64EncodedSecretKey)

Returns: SecretKey

Decodes a base64-encoded key and wraps it as a SecretKey for the given algorithm, the same as createSecretKeyFromBytes.

ParameterDescription
algorithmthe key algorithm name, for example AES
base64EncodedSecretKeythe key, base64-encoded
To get full access to the Kademi Hub existing customers can login here, or new customers can register here.