HASH scalar function

The HASH function returns a 128-bit, 160-bit, 256-bit or 512-bit hash of the input data, depending on the algorithm selected, and is intended for cryptographic purposes.

Read syntax diagramSkip visual syntax diagramHASH(string-expression ,0,algorithm )

The schema is SYSIBM.

string-expression
An expression that represents the string value to be hashed. This expression must return a built-in character string, graphic string, binary string, numeric value, Boolean value, or datetime value. If the value is not a character, graphic, or binary string, it is implicitly cast to VARCHAR before the function is evaluated.
algorithm
An expression that returns a value that indicates which algorithm is to be used for hashing. The expression must return a value that has a built-in numeric, a CHAR, or a VARCHAR data type. In a Unicode database, the expression can also return a GRAPHIC or VARGRAPHIC data type. If the value is not an INTEGER, it is cast to INTEGER before the function is evaluated. If no algorithm is specified, the default algorithm value of 0 is used.
Table 1 shows the algorithm used, the result size, and the number of return values for each algorithm value.
Table 1. Resulting size from each algorithm
Algorithm value Algorithm Result size Number of return values
0 MD5 128 bit 2128
1 SHA1 160 bit 2160
2 SHA2_256 256 bit 2256
3 SHA2_512 512 bit 2512
Note that security flaws have been identified in both the SHA1 and MD5 algorithms. You can find acceptable hash algorithms in applicable compliance documentation, such as National Institute of Standards and Technology (NIST) Special Publication 800-131A.

Result

The data type of the result is VARBINARY. If any argument can be null, the result can be null. If any argument is null, the result is the null value.

Example

values  hash('Charlie at IBM', 1)
result is Ãä#F$Âüin¬SðÂÂ¥G5§