This module provide functions that perform encryptions through common algorithms, such as MD5, SHA1, and so on.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::Crypto
to the header of your
DataWeave script.
Functions
HMACBinary
HMACBinary(Binary, Binary, String): Binary
Computes an HMAC hash (with a secret cryptographic key) on input content.
See also, HMACWith
.
Parameters
Name | Description |
---|---|
|
The secret cryptographic key (a |
|
The input content, a |
|
The hashing algorithm. By default HmacSHA1 is used. |
Example
This example uses HMAC with a secret value to encrypt the input content.
Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "HMACBinary" : Crypto::HMACBinary("key_re_loca" as Binary, "xxxxx" as Binary) }
Output
1
{ "HMACBinary": ".-\ufffd\ufffd\u0012\ufffdۊ\ufffd\ufffd\u0000\ufffd\u0012\u0018R\ufffd\ufffd=\ufffd*" }
HMACWith
HMACWith(Binary, Binary, String): String
Computes an HMAC hash (with a secret cryptographic key) on input content, then transforms the result into a lowercase, hexadecimal string.
See also, HMACBinary
.
Parameters
Name | Description |
---|---|
|
The secret cryptographic key (a |
|
The input content, a |
|
(Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.) The hashing algorithm. By default, |
Example
This example uses HMAC with a secret value to encrypt the input content using the HmacSHA256
algorithm.
Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "HMACWith" : Crypto::HMACWith("secret_key" as Binary, "Some value to hash" as Binary, "HmacSHA256") }
Output
1
{ "HMACWith": "b51b4fe8c4e37304605753272b5b4321f9644a9b09cb1179d7016c25041d1747" }
MD5
MD5(Binary): String
Computes the MD5 hash and transforms the binary result into a hexadecimal lower case string.
Parameters
Name | Description |
---|---|
|
A binary input value to encrypt. |
Example
This example uses the MD5 algorithm to encrypt a binary value.
Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "md5" : Crypto::MD5("asd" as Binary) }
Output
1
{ "md5": "7815696ecbf1c96e6894b779456d330e" }
SHA1
SHA1(Binary): String
Computes the SHA1 hash and transforms the result into a hexadecimal, lowercase string.
Parameters
Name | Description |
---|---|
|
A binary input value to encrypt. |
Example
This example uses the SHA1 algorithm to encrypt a binary value.
Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "sha1" : Crypto::SHA1("dsasd" as Binary) }
Output
1
{ "sha1": "2fa183839c954e6366c206367c9be5864e4f4a65" }
hashWith
hashWith(Binary, String): Binary
Computes the hash value of binary content using a specified algorithm.
The first argument specifies the binary content to use to calculate the hash value, and the second argument specifies the hashing algorithm to use. The second argument must be any of the accepted Algorithm names:
Algorithm names | Description |
---|---|
|
The MD2 message digest algorithm as defined in RFC 1319. |
|
The MD5 message digest algorithm as defined in RFC 1321. |
|
Hash algorithms defined in the FIPS PUB 180-2. SHA-256 is a 256-bit hash function intended to provide 128 bits of security against collision attacks, while SHA-512 is a 512-bit hash function intended to provide 256 bits of security. A 384-bit hash may be obtained by truncating the SHA-512 output. |
Parameters
Name | Description |
---|---|
|
The binary input content to hash. |
|
The name of the algorithm to use to calculate the hash value of |
Example
This example uses the MD2 algorithm to encrypt a binary value.
Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "md2" : Crypto::hashWith("hello" as Binary, "MD2") }
Output
1
{ "md2": "\ufffd\u0004ls\ufffd\u00031\ufffdh\ufffd}8\u0004\ufffd\u0006U" }