hash()

The hash() function computes the hash value of binary data using a specified hash algorithm and returns it as binary.

Syntax

hash(ALGORITHM, DATA)

Parameters

ALGORITHM
The name of the hash algorithm. Specify one of md5, sha1, sha256, sha384, or sha512.
DATA
The binary data to hash.

Description

The hash() function computes the hash value of binary data using the specified hash algorithm and returns it as binary.

Returns null if DATA is null or not a binary type.

An error is raised at query parse time if an unsupported algorithm is specified or if required parameters are missing.

Error codes

Error codeDescription
90690No hash algorithm was specified.
90691No hash data was specified.
90692An unsupported hash algorithm was specified.

Usage examples

  1. Compute the MD5 hash value

    json "{}" | eval result = tohex(hash("md5", encode("hello, world!")))
    | # result: 3adbbad1791fbae3ec908894c4963870
    
  2. Compute the SHA-256 hash value

    json "{}" | eval result = tohex(hash("sha256", encode("hello, world!")))
    | # result: 68e656b251e67e8358bef8483ab0d51c6619f3e7a1a9f0e75838d41ff368f728
    
  3. Non-binary value as input

    json "{}" | eval result = hash("md5", "hello world")
    | # result: null
    
  4. DATA is null

    json "{}" | eval result = hash("sha1", null)
    | # result: null
    

Compatibility

The hash() function has been available since before Sonar 4.0.