FirebirdSQL logo

CRYPT_HASH()

Cryptographic hash

Result type

VARBINARY

Syntax
CRYPT_HASH (value USING <hash>)

<hash> ::= MD5 | SHA1 | SHA256 | SHA512
Table 1. CRYPT_HASH Function Parameter
Parameter Description

value

Expression of value of any type;non-string or non-binary types are converted to string

hash

Cryptographic hash algorithm to apply

CRYPT_HASH returns a cryptographic hash calculated from the input argument using the specified algorithm.If the input argument is not a string or binary type, it is converted to string before hashing.

This function returns a VARBINARY with the length depending on the specified algorithm.

Note
  • The MD5 and SHA1 algorithms are not recommended for security purposes due to known attacks to generate hash collisions.These two algorithms are provided for backward-compatibility only.

  • When hashing string or binary values, take into account the effects of trailing blanks (spaces or NULs).The value 'ab' in a CHAR(5) (3 trailing spaces) has a different hash than if it is stored in a VARCHAR(5) (no trailing spaces) or CHAR(6) (4 trailing spaces).

    To avoid this, make sure you always use a variable length data type, or the same fixed length data type, or normalize values before hashing, for example using TRIM(TRAILING FROM value).

Examples of CRYPT_HASH

Hashing x with the SHA512 algorithm
select crypt_hash(x using sha512) from y;