All Products
Search
Document Center

MaxCompute:SHA2

Last Updated:Jul 24, 2023

Calculates the SHA-2 hash value of expr that is of the STRING or BINARY type and returns the SHA-2 hash value in the format specified by number.

Syntax

string sha2(string|binary <expr>, bigint <number>)

Parameters

  • expr: required. A value of the STRING or BINARY type.

  • number: required. A value of the BIGINT type. This parameter specifies the hash bit length, which must be 224, 256, 384, 512, or 0. The value that is returned when this parameter is set to 256 is the same as the value that is returned when this parameter is set to 0.

Return value

A value of the STRING type is returned. The return value varies based on the following rules:

  • An input parameter is set to null.

  • The value of number does not fall in the valid value range.

Examples

  • Example 1: Calculate the SHA-2 hash value of string ABC. Sample statement:

    -- The return value is b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78. 
    select sha2('ABC', 256);
  • Example 2: An input parameter is set to null. Sample statement:

    -- The return value is null. 
    select sha2('ABC', null);

Related functions

For more information, see Other functions.