The `hash`

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

`hash sha256 [HMAC [b16|b64]`

`key] target|{target}`

`as`

`result`

`[b16|b64]`

`hash md5`

`target|{target}`

`as`

`result`

`[b16|b64]`

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 `HMAC`

* key* arguments are provided when the hash type is

`sha256`

`b64`

`b16`

`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`

The optional ** b16** argument (

`base16`

Running the script:

var hash_me = "This is the data to hash"var my_secret = "This is my secret key"â€‹# SHA256hash sha256 hash_me as resultprint The SHA256 hash of '${hash_me}' in base-16 is:print ${result}${NEWLINE}â€‹hash sha256 hash_me as result b64print The SHA256 hash of '${hash_me}' in base-64 is:print ${result}${NEWLINE}â€‹# HMACSHA256hash sha256 hmac ${my_secret} hash_me as resultprint 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 b64print 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=