You can use Handlebars to encode or hash values in your dynamic content. This article describes the various encoding and hashing helpers Iterable supports and how to use them.
# In this article
# Hashing helpers
You can use hashing helpers to generate common hash-based message authentication codes (HMACs) that are useful for encrypting user data for security.
A great use case for this is if you want to automatically populate a web page (like your company's messaging preferences center) with a user's details when they click a link. You need to reference the user's data in order to personalize their experience, but it's best practice not to expose personal data in a URL. With these Handlebars helpers, you can reference a user's personal data while also keeping it secure.
The hashing Handlebars helpers Iterable supports are based on various Secure Hash Algorithms, which differ in terms of the levels of security they provide. The supported hashing helpers are listed in this section from most recent (and most secure) to oldest and least secure.
WARNING
Before using these hashing helpers, be sure that you understand the potential risks associated with doing so. These helpers must not be used as a substitute for a proper authentication system to your platform; authentication should always redirect to your organization's login pages.
If you have any questions about using these Handlebars helpers, reach out to your Iterable customer success manager.
# Generate a SHA-256 HMAC
The hmacSHA256 helper generates a Secure Hash Algorithm 2 (SHA-2) HMAC
hash for the value of the specified user or event data field using your
project's HMAC secret.
Because this helper hashes the value of the specified data field using a more recent standard of the Secure Hashing Algorithm (SHA-256) and requires authentication with a secret key, it provides the highest level of security.
# Properties
fieldName — The name of the user or event data field whose value you want to
encrypt.
# Format
# Example
| Code | Example userId value | Output |
|---|---|---|
| 123456789 | 10ec54aa2d39e7ba838e3c485b46436d70c5f577f3df20f8ba8c5e7559d568dc |
# Generate a SHA-256 hash
The sha256 helper encrypts the value of the specified user or event data field
as a SHA-256 hash.
Because this helper only hashes the value of a data field but does not require
authentication with a secret key, it provides a lower level of security than
the hmacSHA256 helper.
# Properties
fieldName — The name of the user or event data field whose value you want to
encrypt.
# Format
Standard helper:
Block helper:
# Example
| Code | Example email value | Output |
|---|---|---|
| user@example.com | 3a86c6f084291fda367f24e885c74d2f1d50419eb4028d2b1bb2060d8f45ce0b |
# Generate a SHA-1 HMAC
The hmacSHA1 helper generates a Secure Hash Algorithm 1 (SHA-1) HMAC
hash for the value of the specified user or event data field using your
project's HMAC secret.
Because this helper hashes the value of the specified data field and requires
authentication with a secret key, it provides a higher level of security than
the sha1 helper. However, it uses an older (and less secure) standard of the
Secure Hash Algorithm. When possible, we recommend using SHA-256.
# Properties
fieldName — The name of the user or event data field whose value you want to
encrypt.
# Format
# Example
| Code | Example userId value | Output |
|---|---|---|
| 123456789 | 310cf1d869f57ad61fc46d45fc9496fa1a628162 |
# Generate a SHA-1 hash
The sha1 helper generates a Secure Hash Algorithm 1 (SHA-1)
hash for the value of the specified user or event data field.
NOTE
Security experts regard the SHA-1 hashing algorithm as outdated and advise against using it due to vulnerabilities. (However, it is more secure when used with an HMAC.) Consider more secure, up-to-date alternatives like SHA-256.
# Properties
fieldName — The name of the user or event data field whose value you want to
encrypt.
# Format
Standard helper:
Block helper:
# Example
| Code | Example email value | Output |
|---|---|---|
| user@example.com | 63a710569261a24b3766275b7000ce8d7b32e2f7 |
# Generate an MD5 hash
The md5 helper generates an MD5 hash.
NOTE
Security experts regard the MD5 hashing algorithm as outdated and advise against using it due to vulnerabilities. (However, it is more secure when used with an HMAC.) Consider more secure, up-to-date alternatives like SHA-256.
# Properties
fieldName — The name of the user or event data field whose value you want to
encrypt.
# Format
Standard helper:
Block helper:
# Example
| Code | Example email value | Output |
|---|---|---|
| user@example.com | b58996c504c5638798eb6b511e6f49af |
# Encoding helpers
You can use encoding helpers to change the representation of values in your dynamic content for use in different formats like Base64, JSON, and URLs.
# Convert a value to Base64
The #base64 helper converts a value to Base64.
# Properties
fieldName — The name of the data field whose value should be converted to Base64.
# Format
# Example
| Code | Example email value | Output |
|---|---|---|
| user@example.com | ZG9jc0BpdGVyYWJsZS5jb20= |
# Format a value as JSON
The toJson helper formats a value for use in a JSON field.
# Properties
fieldName — The name of the data field whose value should be formatted as JSON.
# Format
# Example
| Code | Example drink value | Output |
|---|---|---|
| piña colada | "piña colada" |
NOTE
The output of the toJson helper depends on the data type of fieldName.
Value of fieldName
| Data type | Output |
|---|---|---|
Example string | String | "Example string" |
1234 | Long | 1234 |
3.14 | Float | 3.14 |
{"field1": "value"} | Object | {"field1": "value"} |
[1,2,3,4] | Array | [1,2,3,4] |
# Format a value as a URL-encoded JSON string
The toUrlEncodedJson helper formats a value for use in a JSON field. Special
characters like quotes (""), curly braces ({}), and square brackets ([])
are encoded as they would be for use in a URL.
# Properties
fieldName — The name of the data field whose value should be formatted as a
URL-encoded JSON string.
# Format
# Example
| Code | Example drink value | Output |
|---|---|---|
| piña colada | %22pi%C3%B1a+colada%22 |
NOTE
With this helper, the output depends on the data type
of fieldName.
Value of fieldName
| Data type | Output |
|---|---|---|
Example string | String | %22Example+string%22 |
1234 | Long | 1234 |
3.14 | Float | 3.14 |
{"field1": "value"} | Object | %7B%22field1%22%3A%22value%22%7D |
[1,2,3,4] | Array | %5B1%2C2%2C3%2C4%5D |
# Format a value for use in a URL
The #urlEncode block helper converts a string (or the value of a data field
containing a string) to standard URL formatting.
Spaces are replaced with plus signs (+), and special characters (for example,
ñ) are converted to their ASCII
equivalent.
# Properties
fieldName — The name of the data field whose value should be formatted for use
in a URL.
# Format
# Example
| Code | Example drink value | Output |
|---|---|---|
| piña colada | pi%C3%B1a+colada |
# Hex-encode a value
The hexEncode helper encodes the value of a user or event data field as a
hexadecimal string.
# Properties
fieldName — The name of the data field whose value should be hex-encoded.
# Format
Standard helper:
Block helper:
# Example
| Code | Example email value | Output |
|---|---|---|
| user@example.com | 75736572406578616d706c652e636f6d |
# Want to learn more?
For more information about some of the topics in this article, check out these resources. Iterable Academy is open to everyone — you don't need to be an Iterable customer!
Iterable Academy
Support docs