跳到主要内容

SHA2

描述

使用 SHA2 对信息进行摘要处理。

语法

SHA2(<input>, <digest_length>)

参数

参数说明
<input>待加密的内容, 接受字符串和二进制类型
<digest_length>摘要长度,支持 224, 256, 384, 512,必须为常量

返回值

返回输入字符串的 sha2 值

示例

select sha2('abc', 224), sha2('abc', 384), sha2(NULL, 512);
+----------------------------------------------------------+--------------------------------------------------------------------------------------------------+-----------------+
| sha2('abc', 224) | sha2('abc', 384) | sha2(NULL, 512) |
+----------------------------------------------------------+--------------------------------------------------------------------------------------------------+-----------------+
| 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7 | cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7 | NULL |
+----------------------------------------------------------+--------------------------------------------------------------------------------------------------+-----------------+
select sha2('abc', 225);
ERROR 1105 (HY000): errCode = 2, detailMessage = sha2 functions only support digest length of [224, 256, 384, 512]
select sha2('str', k1) from str;
ERROR 1105 (HY000): errCode = 2, detailMessage = the second parameter of sha2 must be a literal but got: k1
select sha2(k0, k1) from str;
ERROR 1105 (HY000): errCode = 2, detailMessage = the second parameter of sha2 must be a literal but got: k1
-- vb (VarBinary) 和 vc (VarChar) 插入时使用了相同的字符串.
SELECT * FROM mysql_catalog.binary_test.binary_test;
+------+------------+------+
| id | vb | vc |
+------+------------+------+
| 1 | 0x616263 | abc |
| 2 | 0x78797A | xyz |
| 3 | NULL | NULL |
+------+------------+------+
SELECT SHA2(vb, 224), SHA2(vc, 224) FROM mysql_catalog.binary_test.binary_test;
+----------------------------------------------------------+----------------------------------------------------------+
| SHA2(vb, 224) | SHA2(vc, 224) |
+----------------------------------------------------------+----------------------------------------------------------+
| 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7 | 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7 |
| 30e90f1cd0ceff8eb3dd6a540a605c0666f841d35de63c57e4dd2877 | 30e90f1cd0ceff8eb3dd6a540a605c0666f841d35de63c57e4dd2877 |
| NULL | NULL |
+----------------------------------------------------------+----------------------------------------------------------+