SHA256 Function

Overview

The SHA256 function computes the SHA-256 hash value for a given string. SHA-256 is the most commonly used variant of the SHA-2 family of hash algorithms, capable of mapping arbitrary-length data to a fixed-length (256-bit) hash value, outputting a 64-character hexadecimal string.

Syntax

sha256(expr)

Parameters

  • expr: The input data for which to compute the SHA-256 hash value. Supports STRING, VARCHAR, CHAR, and BINARY types.

Return Result

Returns a STRING type value representing the computed 64-character hexadecimal hash string.

Examples

  1. Compute the SHA-256 value of a simple string:

    SELECT sha256('hello') AS res; +------------------------------------------------------------------+ | res | +------------------------------------------------------------------+ | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 | +------------------------------------------------------------------+

  2. Compute the SHA-256 value of a string containing special characters:

    SELECT sha256('Hello, World!') AS res; +------------------------------------------------------------------+ | res | +------------------------------------------------------------------+ | dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f | +------------------------------------------------------------------+

  3. When the input is NULL:

    SELECT sha256(NULL) AS res; +------+ | res | +------+ | NULL | +------+

Notes

  • SHA-256 hash values are irreversible, meaning the original data cannot be derived from the hash value.
  • SHA-256 outputs a fixed 64-character hexadecimal string (256 bits).
  • When the input parameter is NULL, the result is NULL.
  • SHA-256 belongs to the SHA-2 family of algorithms and is one of the most widely used secure hash algorithms today, suitable for data integrity verification, digital signatures, and other scenarios.