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): Binary

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

See also, HMACWith.

Parameters

Name Description

content

The input content, a Binary value.

secret

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

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
---
Crypto::HMACBinary("MySecret" as Binary, "MyContent" as Binary)

Output

1
 "ӝ\ufffd\r{\u0017\u0004v\ufffd\ufffd\n\ufffd\\p\ufffd\ufffd6\ufffd@\ufffd"

HMACWith

HMACWith(Binary, Binary): 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

content

The input content, a Binary value.

secret

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

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
---
Crypto::HMACWith("MySecret" as Binary, "My Content To Hash" as Binary)

Output

1
"2999d8ec1674b0f27c16510855a622dfabb3dada"

MD5

MD5(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
---
Crypto::MD5("asd" as Binary)

Output

1
2
3
4
%dw 2.0
import dw::Crypto
---
"7815696ecbf1c96e6894b779456d330e"

SHA1

SHA1(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
---
Crypto::SHA1("dsasd" as Binary)

Output

1
"2fa183839c954e6366c206367c9be5864e4f4a65"

hashWith

hashWith(Binary, String): Binary

Computes the hash of binary content with a specified algorithm.

You can use these names as algorithm arguments:

Name Description

MD2

The MD2 message digest algorithm as defined in RFC 1319[http://www.ietf.org/rfc/rfc1319.txt].

MD5

The MD5 message digest algorithm as defined in RFC 1321[http://www.ietf.org/rfc/rfc1321.txt].

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

Hash algorithms defined in the FIPS PUB 180-2 [http://csrc.nist.gov/publications/fips/index.html]. 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

A binary input value to encrypt.

algorithm

The name of the algorithm to use for encrypting the 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
---
Crypto::hashWith("hello" as Binary, "SHA-256")

Output

1
 "\ufffd\u0004ls\ufffd\u00031\ufffdh\ufffd}8\u0004\ufffd\u0006U"