set_hmac_sha1

The `set_hmac_sha1` directive computes the HMAC SHA1 hash of a given input with a specified key.

Syntaxset_hmac_sha1 $output_variable $key $input_string;
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments3

Description

The set_hmac_sha1 directive from the NGINX Set-Misc module allows users to generate an HMAC (Hash-based Message Authentication Code) using the SHA1 hash algorithm. This is particularly useful for creating secure tokens or signatures for API requests or messages that require integrity verification. The directive requires three arguments: the variable to store the output, the key used for the HMAC, and the input string that is to be hashed.

When executed, the set_hmac_sha1 directive will take the provided input string and compute its HMAC using the secret key. The resulting hash is then stored in the specified output variable. This operation could be beneficial in scenarios where the authenticity of data needs to be validated, or when signing payloads against tampering during transmission. It's important to note that the output will vary based on the key and input used, emphasizing the importance of keeping the key secret and secure.

The directive can be used in several contexts, including http, server, location, and if blocks within those contexts. This versatility provides flexibility in how the HMAC can be applied throughout the NGINX configuration depending on your specific application needs.

Config Example

location /secure-endpoint {
    set $secret_key 'yoursecret';
    set_hmac_sha1 $auth_token $secret_key $request_body;
}

Ensure the key used is sufficiently random and secret to prevent unauthorized data access.

The input must be a string; ensure the data type of the input is appropriate before passing it to the directive.

Be cautious about using this in high-traffic contexts, as HMAC calculations can introduce latency.

← Back to all directives