hash

The hash statement is used to generate a base-16 or base-64 encoded hash of data stored in a variable or named buffer.

Syntax

hash sha256 [HMAC [b16|b64]key] target|{target}asresult[b16|b64]

hash md5target|{target}asresult[b16|b64]

Details

The hash statement uses the contents of target as its input and places the final result into result. The SHA256 and MD5 hash algorithms are supported.

If target is surrounded with curly braces like {this} then it is taken to be the name of a memory buffer and the contents of the buffer will be used as input. Otherwise, it is treated as the name of the variable, the value of which will be hashed.

By default the resulting hash is base-16 encoded and the result placed into the variable specified by the result argument.

result is the name of the variable to put the output into, and not a reference to the contents of that variable. This is why it is not ${result}

If the optional HMACkey arguments are provided when the hash type is sha256 then the secret in key will be used to generate an HMAC-SHA-256 result. The optional b64 or b16 argument following the HMAC option indicates that key is base-64 or base-16 encoded. By default, a clear-text key is assumed.

If the optional b64 argument is used (base64 may also be specified) after the result variable, then the result will be base-64 encoded.

The optional b16 argument (base16 may also be used) after the result variable is provided for completeness, but need not be specified as this is the default encoding to use.

Example

Running the script:

var hash_me = "This is the data to hash"
var my_secret = "This is my secret key"

# SHA256
hash sha256 hash_me as result
print The SHA256 hash of '${hash_me}' in base-16 is:
print ${result}${NEWLINE}

hash sha256 hash_me as result b64
print The SHA256 hash of '${hash_me}' in base-64 is:
print ${result}${NEWLINE}

# HMACSHA256
hash sha256 hmac ${my_secret} hash_me as result
print The HMACSHA256 hash of '${hash_me}' (using '${my_secret}') in base-16 is:
print ${result}${NEWLINE}

hash sha256 hmac ${my_secret} hash_me as result b64
print The HMACSHA256 hash of '${hash_me}' (using '${my_secret}') in base-64 is:
print ${result}${NEWLINE}

results in the following output:

The SHA256 hash of 'This is the data to hash' in base-16 is:
1702c37675c14d0ea99b7c23ec29c36286d1769a9f65212218d4380534a53a7a

The SHA256 hash of 'This is the data to hash' in base-64 is:
FwLDdnXBTQ6pm3wj7CnDYobRdpqfZSEiGNQ4BTSlOno=

The HMACSHA256 hash of 'This is the data to hash' (using 'This is my secret key') in base-16 is:
cf854e99094ea5c2a88ee0901a305d5f25dfb5a0f0905eec703618080567b4b5

The HMACSHA256 hash of 'This is the data to hash' (using 'This is my secret key') in base-64 is:
z4VOmQlOpcKojuCQGjBdXyXftaDwkF7scDYYCAVntLU=

Last updated