Encryption functions
These functions implement encryption and decryption of data with AES (Advanced Encryption Standard) algorithm.
The key length depends on the encryption mode: 16, 24, and 32 bytes long for -128-, -196-, and -256- modes respectively.
The initialization vector length is always 16 bytes (bytes in excess of 16 are ignored).
HMAC
Introduced in: v25.12
Computes the HMAC (Hash-based Message Authentication Code) for the given message using the specified hash algorithm and secret key.
Supported hash algorithms:
- RSA-MD4 (aliases: MD4, RSA-MD4)
- RSA-MD5 (aliases: MD5, RSA-MD5)
- RSA-MDC2 (aliases: MDC2, RSA-MDC2)
- RSA-RIPEMD160 (aliases: RIPEMD160, RSA-RIPEMD160)
- RSA-SHA1 (aliases: RSA-SHA1, SHA1)
- RSA-SHA1-2 (aliases: RSA-SHA1, RSA-SHA1-2)
- RSA-SHA224 (aliases: RSA-SHA224, SHA224)
- RSA-SHA256 (aliases: RSA-SHA256, SHA256)
- RSA-SHA3-224 (aliases: RSA-SHA3-224, SHA3-224)
- RSA-SHA3-256 (aliases: RSA-SHA3-256, SHA3-256)
- RSA-SHA3-384 (aliases: RSA-SHA3-384, SHA3-384)
- RSA-SHA3-512 (aliases: RSA-SHA3-512, SHA3-512)
- RSA-SHA384 (aliases: RSA-SHA384, SHA384)
- RSA-SHA512 (aliases: RSA-SHA512, SHA512)
- RSA-SHA512/224 (aliases: RSA-SHA512/224, SHA512-224)
- RSA-SHA512/256 (aliases: RSA-SHA512/256, SHA512-256)
- RSA-SM3 (aliases: RSA-SM3, SM3)
- blake2b512
- blake2s256
- id-rsassa-pkcs1-v1_5-with-sha3-224 (aliases: SHA3-224, id-rsassa-pkcs1-v1_5-with-sha3-224)
- id-rsassa-pkcs1-v1_5-with-sha3-256 (aliases: SHA3-256, id-rsassa-pkcs1-v1_5-with-sha3-256)
- id-rsassa-pkcs1-v1_5-with-sha3-384 (aliases: SHA3-384, id-rsassa-pkcs1-v1_5-with-sha3-384)
- id-rsassa-pkcs1-v1_5-with-sha3-512 (aliases: SHA3-512, id-rsassa-pkcs1-v1_5-with-sha3-512)
- md4
- md4WithRSAEncryption (aliases: MD4, md4WithRSAEncryption)
- md5
- md5-sha1
- md5WithRSAEncryption (aliases: MD5, md5WithRSAEncryption)
- mdc2
- mdc2WithRSA (aliases: MDC2, mdc2WithRSA)
- ripemd (aliases: RIPEMD160, ripemd)
- ripemd160
- ripemd160WithRSA (aliases: RIPEMD160, ripemd160WithRSA)
- rmd160 (aliases: RIPEMD160, rmd160)
- sha1
- sha1WithRSAEncryption (aliases: SHA1, sha1WithRSAEncryption)
- sha224
- sha224WithRSAEncryption (aliases: SHA224, sha224WithRSAEncryption)
- sha256
- sha256WithRSAEncryption (aliases: SHA256, sha256WithRSAEncryption)
- sha3-224
- sha3-256
- sha3-384
- sha3-512
- sha384
- sha384WithRSAEncryption (aliases: SHA384, sha384WithRSAEncryption)
- sha512
- sha512-224
- sha512-224WithRSAEncryption (aliases: SHA512-224, sha512-224WithRSAEncryption)
- sha512-256
- sha512-256WithRSAEncryption (aliases: SHA512-256, sha512-256WithRSAEncryption)
- sha512WithRSAEncryption (aliases: SHA512, sha512WithRSAEncryption)
- shake128
- shake256
- sm3
- sm3WithRSAEncryption (aliases: SM3, sm3WithRSAEncryption)
- ssl3-md5 (aliases: MD5, ssl3-md5)
- ssl3-sha1 (aliases: SHA1, ssl3-sha1)
- whirlpool
Syntax
Arguments
mode— Hash algorithm name (case-insensitive). Supported: md5, sha1, sha224, sha256, sha384, sha512.Stringmessage— Message to be authenticated.Stringkey— Secret key for HMAC.String
Returned value
Returns a binary string containing the HMAC digest. String
Examples
Basic HMAC-SHA256
Different hash algorithms
Case-insensitive mode
aes_decrypt_mysql
Introduced in: v20.12
Decrypts data encrypted by MySQL's AES_ENCRYPT function.
Produces the same plaintext as decrypt for the same inputs.
When key or iv are longer than they should normally be, aes_decrypt_mysql will stick to what MySQL's aes_decrypt does which is to 'fold' key and ignore the excess bits of IV.
Supports the following decryption modes:
- aes-128-ecb, aes-192-ecb, aes-256-ecb
- aes-128-cbc, aes-192-cbc, aes-256-cbc
- aes-128-cfb128
- aes-128-ofb, aes-192-ofb, aes-256-ofb
Syntax
Arguments
mode— Decryption mode.Stringciphertext— Encrypted text that needs to be decrypted.Stringkey— Decryption key.Stringiv— Optional. Initialization vector.String
Returned value
Returns the decrypted String. String
Examples
Decrypt MySQL data
aes_encrypt_mysql
Introduced in: v20.12
Encrypts text the same way as MySQL's AES_ENCRYPT function does.
The resulting ciphertext can be decrypted with MySQL's AES_DECRYPT function.
Produces the same ciphertext as the encrypt function for the same inputs.
When key or iv are longer than they should normally be, aes_encrypt_mysql will stick to what MySQL's aes_encrypt does which is to 'fold' key and ignore the excess bits of iv.
The supported encryption modes are:
- aes-128-ecb, aes-192-ecb, aes-256-ecb
- aes-128-cbc, aes-192-cbc, aes-256-cbc
- aes-128-ofb, aes-192-ofb, aes-256-ofb
Syntax
Arguments
mode— Encryption mode.Stringplaintext— Text that should be encrypted.Stringkey— Encryption key. If the key is longer than required bymode, MySQL-specific key folding is performed.Stringiv— Optional. Initialization vector. Only the first 16 bytes are taken into account.String
Returned value
Ciphertext binary string. String
Examples
Equal input comparison
Encrypt fails with long key
MySQL compatibility
Longer IV produces the same result
decrypt
Introduced in: v20.12
This function decrypts an AES-encrypted binary string using the following modes:
- aes-128-ecb, aes-192-ecb, aes-256-ecb
- aes-128-cbc, aes-192-cbc, aes-256-cbc
- aes-128-ofb, aes-192-ofb, aes-256-ofb
- aes-128-gcm, aes-192-gcm, aes-256-gcm
- aes-128-ctr, aes-192-ctr, aes-256-ctr
- aes-128-cfb, aes-128-cfb1, aes-128-cfb8
Syntax
Arguments
mode— Decryption mode.Stringciphertext— Encrypted text that should be decrypted.Stringkey— Decryption key.Stringiv— Initialization vector. Required for-gcmmodes, optional for others.Stringaad— Additional authenticated data. Won't decrypt if this value is incorrect. Works only in-gcmmodes, for others throws an exception.String
Returned value
Returns decrypted plaintext. String
Examples
Correctly decrypting encrypted data
Incorrectly decrypting encrypted data
encrypt
Introduced in: v20.12
Encrypts plaintext into ciphertext using AES in one of the following modes:
- aes-128-ecb, aes-192-ecb, aes-256-ecb
- aes-128-cbc, aes-192-cbc, aes-256-cbc
- aes-128-ofb, aes-192-ofb, aes-256-ofb
- aes-128-gcm, aes-192-gcm, aes-256-gcm
- aes-128-ctr, aes-192-ctr, aes-256-ctr
- aes-128-cfb, aes-128-cfb1, aes-128-cfb8
Syntax
Arguments
mode— Encryption mode.Stringplaintext— Text that should be encrypted.Stringkey— Encryption key.Stringiv— Initialization vector. Required for-gcmmodes, optional for others.Stringaad— Additional authenticated data. It isn't encrypted, but it affects decryption. Works only in-gcmmodes, for others it throws an exception.String
Returned value
Returns binary string ciphertext. String
Examples
Example encryption
Example with GCM mode
tryDecrypt
Introduced in: v22.10
Similar to the decrypt function, but returns NULL if decryption fails when using the wrong key.
Syntax
Arguments
mode— Decryption mode.Stringciphertext— Encrypted text that should be decrypted.Stringkey— Decryption key.Stringiv— Optional. Initialization vector. Required for-gcmmodes, optional for other modes.Stringaad— Optional. Additional authenticated data. Won't decrypt if this value is incorrect. Works only in-gcmmodes, for other modes throws an exception.String
Returned value
Returns the decrypted String, or NULL if decryption fails. Nullable(String)
Examples
Create table and insert data