HMAC_SHA1

The HMAC_SHA1 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-SHA1 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-1 algorithm

Declaration

HMAC_SHA1(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_SHA! will return the Hex-encoded hash. If a value other than "Hex" or "Base64" is given for encode, HMAC_SHA1 will throw an error.

Return Values

hash(type: string)
The hash of the input as calculated by the HMAC-SHA-1 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-SHA1 algorithm:

HMAC_SHA1("This is a string.", "111") => "78a6c59e2712dcee89d25e1ccb1884c316f2ea11"

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-SHA1 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_SHA1("This is a string.", "111", "Hex") => "78a6c59e2712dcee89d25e1ccb1884c316f2ea11"

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

HMAC_SHA1("This is a string.", "111", "Base64") => "eKbFnicS3O6J0l4cyxiEwxby6hE="

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

HMAC_SHA1("This is a string.", "ai4nga0") => "c998cce59510feaea598bbc4272aa7768201c7b3"