Implementing Dataweave Crypto With Mulesoft
MuleSoft provides various functions to encrypt the fields within Dataweave transformation and it can be achieved using various algorithms like MD5, SHA1, etc.
Join the DZone community and get the full member experience.
Join For FreeMuleSoft provides various functions to encrypt the fields within Dataweave transformation and it can be achieved using various algorithms like MD5, SHA1, etc.
To use Crypto in the Datawave, one must import Crypto by using import dw::Crypto
HMACBinary
HMACBinary function will compute the HMAC hash with a Cryptographic Secret key on input data.
Parameter | Description |
secret | Cryptographic key in binary format for encryption. |
content | Input Data or content in Binary format. |
algorithm | Hashing Algorithm. By default, HmacSHA1 is used. |
Example
Input
%dw 2.0
import dw::Crypto
output application/json
---
{
HMACBinary: Crypto::HMACBinary("632423477" as Binary,"Test123456" as Binary,"HmacSha256")
}
Output
xxxxxxxxxx
{
"HMACBinary": "\u0002\u0014=\ufffdg\u0017\ufffd\u001a\u0006M\u0007\ufffd%\ufffd\ufffdʝ%\ufffdt\ufffd\ufffd\ufffd\u001d\ufffds\ufffd\ufffd\u000bž)"
}
HMACWith
HMACWith function will compute the HMAC hash with a Cryptographic Secret key on input data and transform the message into lowercase and hexadecimal string.
Parameter | Description |
secret | Cryptographic key in binary format for encryption. |
content | Input Data or content in Binary format. |
algorithm | Hashing Algorithm. By default HmacSHA1 is used. |
Example
Input
x
%dw 2.0
import dw::Crypto
output application/json
---
{
HMACWith: Crypto::HMACWith("632423477" as Binary,"Test123456" as Binary,"HmacSha512")
}
Output
xxxxxxxxxx
{
"HMACWith": "e4a249fd0f737e0c455d1f250e94312d33f839a5a22f4cb66abeca3a93ff5d4039eb0e685490fc3a3b34bab371f9e255f00f7ac533b8d5175e4bd0cd6a3ad43b"
}
MD5
MD5 function will compute the MD5 hash and transform the binary message into lowercase and hexadecimal string.
Parameter | Description |
content | Input Data or content in Binary format. |
Example
Input
xxxxxxxxxx
%dw 2.0
import dw::Crypto
output application/json
---
{
MD5: Crypto::MD5("632423477" as Binary)
}
Output
xxxxxxxxxx
{
"MD5": "de1c3e94be10bd302f8728cef823970c"
}
SHA1
SHA1 function will compute the SHA1 hash and transform the message into lowercase and hexadecimal string.
Parameter | Description |
content | Input Data or content in Binary format. |
Example
Input
xxxxxxxxxx
%dw 2.0
import dw::Crypto
output application/json
---
{
SHA1: Crypto::SHA1("632423477" as Binary)
}
Output
xxxxxxxxxx
{
"SHA1": "e7224fd2a1b66f04759bfddba8c244d52d64675a"
}
HashWith
HashWith function will compute hash depending on the algorithm provided.
Parameter | Description |
content | Input Data or content in Binary format. |
algorithm | Supported algorithms are SHA1, SHA256, SHA384, SHA512, MD2, MD5 etc. |
Example
Input
xxxxxxxxxx
%dw 2.0
import dw::Crypto
output application/json
---
{
HashWith: Crypto::hashWith("632423477" as Binary,"MD2")
}
Output
xxxxxxxxxx
{
"HashWith": ",s\ufffds\u0013\ufffd\ufffd\ufffd\ufffd\ufffd1*`gc\ufffd"
}
Now, you know that how we can implement the Crypto module in dataweave transformation.
Opinions expressed by DZone contributors are their own.
Comments