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
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:
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:
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:
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:
Last updated