Produces a fixed-length hash value expression.
hash(expression , [algorithm])
Algorithm |
Results in |
---|---|
hash(expression, ‘md5’) |
A varchar 32-byte string. md5 (Message Digest Algorithm 5) is the cryptographic hash function with a 128-bit hash value. |
hash(expression) |
A varchar 32-byte string |
hash(expression, ‘sha1’) |
A varchar 40-byte string sha1 (Secure Hash Algorithm) is the cryptographic hash function with a 160-bit hash value. |
hash(expression, 2) |
A varbinary 16-byte value (using the md5 algorithm) |
hash(expression, 3) |
A varbinary 20-byte value (using the sha1 algorithm) |
update atable set tamper_seal=hash(convert(varchar(30), id) + sensitive_field+@salt, 'sha1')
When specified as a character literal, algorithm is not case-sensitive—“md5”, “Md5” and “MD5” are equivalent. However, if expression is specified as a character datatype then the value is case sensitive. “Time,” “TIME,” and “time” produce different hash values.
If algorithm is a character literal, the result is a varchar string. For “md5” this is a 32-byte string containing the hexadecimal representation of the 128-bit result of the hash calculation. For “sha1” this is a 40-byte string containing the hexadecimal representation of the 160-bit result of the hash calculation.
If algorithm is an integer literal, the result is a varbinary value. For 2, this is a 16-byte value containing the 128-bit result of the hash calculation. For 3, this is a 20-byte value containing the 160-bit result of the hash calculation.
Individual bytes that form expression are fed into the hash algorithm in the order they appear in memory. For many datatypes order is significant. For example, the binary representation of the 4-byte INT value 1 will be 0x00, 0x00, 0x00, 0x01 on MSB-first (big-endian) platforms and 0x01, 0x00, 0x00, 0x00 on LSB-first (little-endian) platforms. Because the stream of bytes is different between platforms, the hash value is different as well. Use hashbytes function to achieve platform independent hash value.
SQL92- and SQL99-compliant
Any user can execute hash.