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 input content, a |
|
The secret cryptographic key (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 input content, a |
|
The secret cryptographic key (a |
|
(Since Version: 2.2.0) 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.
Source
1
2
3
4
5
%dw 2.0
import dw::Crypto
output application/json
---
{ "HMACWith" : Crypto::HMACWith("key_re_loca" as Binary, "xxxxx" as Binary) }
Output
1
{ "HMACWith": "2e2da2e51286db8afa9900f51218529cda3dd32a" }
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 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 |
---|---|
|
A binary input value to encrypt. |
|
The name of the algorithm to use for encrypting the |
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, "SHA-256") }
Output
1
{ "md2": "\ufffd\u0004ls\ufffd\u00031\ufffdh\ufffd}8\u0004\ufffd\u0006U" }