Encoding and hashing helpers

Encoding, hashing, and secret hashing functions transform content in Handlebars expressions.

The helpers described in this document can hash and otherwise transform strings using standard hashing functions, such as MD5 and SHA256. In many of these cases, you will need to transform the output to various encodings, such as Base64 or Hex.

All functions required to perform these transforms are described in this document. In all cases where strings can be provided, they are converted to UTF-8 bytes.

Encoding

The following helpers can be used to change the encoding of a variable. They are commonly used when transforming a byte array, e.g., as output from a hash function, into readable characters.

Base64 encode

$base64Encode — Given a particular string or byte array, encode to Base64.

Properties

  • content — The string or byte array to encode.
  • pad — Optional. A boolean specifying if the output should be padded. Default is true when unspecified.
FormatExampleOutput
{{$base64Encode content pad=true}}{{$base64Encode "wut"}}d3V0

Base64 decode

$base64Decode — Given a Base64-encoded string, decode to an array of bytes.

Properties

  • content — The Base64 encoded string to decode.
FormatExampleOutput
{{$base64Decode content}}{{$utf8Decode ($base64Decode "d3V0")}}wut

UTF-8 decode

$utf8Decode — Given an array of bytes, decode as UTF-8 text.

Properties

  • content — The bytes to decode.

For an example, see Base64 decode above.

UTF-8 encode

$utf8Encode — Given a string, encode as a UTF-8 byte array.

Properties

  • content — The string to encode.

This function is provided as an inverse of UTF-8 decode.

Hex encode

$hexEncode — Given a string or bytes, encode to a hex representation string.

Properties

  • content — The string or bytes to encode.
FormatExampleOutput
{{$hexEncode content}}{{$hexEncode "wut"}}777574

Hashing

Hashing helpers are provided for the following algorithms:

  • MD5: $md5
  • SHA1: $sha1
  • SHA256: $sha256
  • SHA512: $sha512

All have the same usage but use their specified algorithm to perform the hash. These functions return byte arrays, which should then be Hex or Base64 encoded.

Properties

  • content — The string or byte array to hash.
FormatExampleOutput
{{$<algorithm> content}}{{$hexEncode ($md5 "wut")}}c268120ce3918b1264fe2c05143b5c4b

HMAC

HMAC hash functions are provided for the following common algorithms:

  • MD5: $hmacMd5
  • SHA1: $hmacSha1
  • SHA256: $hmacSha256
  • SHA512: $hmacSha512

All have the same usage but use their specified algorithm to perform the hash. These functions return byte arrays, which should then be Hex or Base64 encoded.

Properties

  • content — The string or byte array to hash.
  • secret — The secret key used to sign the hash.
FormatExampleOutput
{{$hmac<algorithm> content secret=secret}}{{$hexEncode ($hmacSha1 "value" secret="key")}}57443a4c052350a44638835d64fd66822f813319