HMAC_SHA256

The HMAC_SHA256 function takes two Strings as input, a base string and a key, and returns the hash of the base string as a Hex- or Base64-encoded string, arrived at via the HMAC-SHA256 algorithm.

This function requires two Strings as input: a base string and key. As an option, it also accepts another string that defines if Hex or Base64 encoding will to be used to calculate the output: a string that gives the hash of the first input, as calculated by the HMAC-SHA-256 algorithm

Declaration

HMAC_SHA256(base_string, key, encode) -> hash

Parameters

base_string (required, type:string)
Any string.

key (required, type:string)
The key used in the calculation of the HMAC.

encode (optional, type:string, default: "Hex")

The string "Hex" or "Base64". This defines if the hash of the base string will be Hex- or Base64-encoded, respectively.

If no value is given for encode, HMAC_SHA256 will return the Hex-encoded hash. If a value other than "Hex" or "Base64" is given for encode, HMAC_SHA256 will throw an error.

Return Values

hash(type: string)
The hash of the input as calculated by the HMAC-SHA-256 algorithm.

Examples

The following example uses the key "111" to calculate Hex-encoded hash of the string "This is a string." according to the HMAC-SHA256 algorithm:

HMAC_SHA256("This is a string.", "111") => "3a8c0680185742ae314a766f71f42c70a86e4add595ffa97c3ab5262c10fb63f"

The following example also uses the key "111" to return the Hex-encoded hash of the string "This is a string." as calculated by the HMAC-SHA256 algorithm. Note that it returns the same output as the above example; there is no difference in output between explicitly defining "Hex" as the value for encode and not defining encode at all:

HMAC_SHA256("This is a string.", "111", "Hex") => "3a8c0680185742ae314a766f71f42c70a86e4add595ffa97c3ab5262c10fb63f"

The following example uses the key "111" to calculate Base64-encoded hash of the string "This is a string." according to the HMAC-SHA256 algorithm. Note how this differs from the first two examples:

HMAC_SHA256("This is a string.", "111", "Base64") => "OowGgBhXQq4xSnZvcfQscKhuSt1ZX/qXw6tSYsEPtj8="

The following example uses the key "ai4nga0" to calculate Hex-encoded hash of the string "This is a string." according to the HMAC-SHA256 algorithm. Note how the output is impacted by the key:

HMAC_SHA256("This is a string.", "ai4nga0") => "94d4bd29fa98b66a787eb9c0b18c32f0698ab3f0d97327a08b044a8a9f98ac1a"