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(secret: Binary, content: Binary, algorithm: String = "HmacSHA1"): Binary

Computes an HMAC hash (with a secret cryptographic key) on input content.

See also, HMACWith.

Parameters
Name Description

secret

The secret cryptographic key (a Binary) used when encrypting the content).

content

The input content, a Binary value.

algorithm

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(secret: Binary, content: Binary, @Since(version= "2.2.0") algorithm: String = "HmacSHA1"): 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

secret

The secret cryptographic key (a Binary) used when encrypting the content).

content

The input content, a Binary value.

algorithm

(Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.) The hashing algorithm. By default, HmacSHA1 is used. Other valid values are HmacSHA256 and HmacSHA512.

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(content: Binary): String

Computes the MD5 hash and transforms the binary result into a hexadecimal lower case string.

Parameters
Name Description

content

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(content: Binary): String

Computes the SHA1 hash and transforms the result into a hexadecimal, lowercase string.

Parameters
Name Description

content

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(content: Binary, algorithm: String = "SHA-1"): 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

MD2

The MD2 message digest algorithm as defined in RFC 1319.

MD5

The MD5 message digest algorithm as defined in RFC 1321.

SHA-1, SHA-256, SHA-384, SHA-512

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

content

The binary input content to hash.

algorithm

The name of the algorithm to use to calculate the hash value of content. This value is a String. Defaults to SHA-1.

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" }