hash
- Fiona Sargeant (Unlicensed)
- Zoe Baldwin
- Anthony George
- Eleanor Brodie
Function: hash()
Returns a hash-encrypted string, optionally with a salt.
Syntax
hash(algorithm, salt, string, format)
Argument | Type | Description | Example Values |
---|---|---|---|
algorithm | String | The name of the hashing algorithm. Four hashing functions are supported: (see below) | |
|
| ||
|
| ||
|
| ||
|
| ||
salt | String | The salt, password or key. Additional salt characters to add to the end of the string before hashing. | |
string | String | The string to be hashed. Must not be null. | |
format | String | Optional, specifies the output format. |
|
Two output formats are supported. The default is HEX encoding of the bytes. Alternatively, Base64 encoding of the resultant value can be requested using one of the values specified for base64Encode.
Examples
Basic Hash
hash("SHA-256", _NULL, "testValue")
Takes the string testValue, uses SHA-256 algorithm to convert it to a hashed string. No salt is added. Returns the value: 82fe0c834cbea069013c5eb7828e599a693e0d2411887e2ab273271662973082
RS256 JWT Token Generation
do( $header = '{"alg":"RS256","typ":"JWT"}, $payload = '{"sub":"1234567890","name":"John Doe","admin":true,"iat":1516239022}', $key = "-----BEGIN PRIVATE KEY-----\r\n ....", $prefix = base64Encode($header, "JWT")+"."+base64Encode($payload, "JWT"), $prefix + "." + hash("SHA256withRSA", $key, $prefix, "JWT")) )
Generates a RS356 JWT token using a private key and the SHA256withRSA signature hash function.
HS256 JWT Token Generation
do( $header = '{"alg":"HS256","typ":"JWT"}, $payload = '{"sub":"1234567890","name":"John Doe","admin":true,"iat":1516239022}', $key = "0123456789abcdef0123456789abcdef", $prefix = base64Encode($header, "JWT")+"."+base64Encode($payload, "JWT"), $prefix + "." + hash("HmacSHA256", $key, $prefix, "JWT")) )
Constructs an HS256 JWT token using a 32 character key and the HmacSHA256 HMAC hash function.
Additional Information
The hash function from version 10.1 onwards supports passing the values of secret keys (see Secret Key and Local Secret) into the hash value.