DB2 BLOB 是如何做 MD5 的?

How does DB2 BLOB do MD5?

当我使用HASH函数时,sysibm.hex (sysibm.hash (NAME, 0)) 是正常的,但如果'NAME'是BLOB或CLOB则不是。 我没有找到关于支持的答案。我的 MD5 用法有误吗?

Db2 11.5

VALUES HASH(BLOB('1',0)) --it's ok.
VALUES HEX(HASH(BLOB('1',0))) --it's wrong

我希望 blob 是 md5

HASH function 期望字符串表达式作为输入:

An expression that represents the string value to be hashed. This expression must return a built-in character string, graphic string, binary string, numeric value, Boolean value, or datetime value. If the value is not a character, graphic, or binary string, it is implicitly cast to VARCHAR before the function is evaluated.

因此涉及一些隐式转换。我相信这可能是返回的 VARBINARY:

的长度
$ db2 "describe values hash('a',0)"

 Column Information

 Number of columns: 1

 SQL type              Type length  Column name                     Name length
 --------------------  -----------  ------------------------------  -----------
 908   VARBINARY                64  1                                         1

$ db2 "describe values hash(blob('a'),0)"

 Column Information

 Number of columns: 1

 SQL type              Type length  Column name                     Name length
 --------------------  -----------  ------------------------------  -----------
 908   VARBINARY             32672  1                                         1

HEX handles VARBINARY up to 16k:

with a maximum length of 16 336 bytes.

所以这失败了:

db2 "values hex(hash(blob('a'),0))"
SQL0171N  The statement was not processed because the data type, length or 
value of the argument for the parameter in position "1" of routine 
"SYSIBM.HEX" is incorrect. Parameter name: "".  SQLSTATE=42815

但如果我们 trim 它下来,它会好的:

db2 "values hex(substr(hash(blob('a'),0),1,16) )"

1                               
--------------------------------
0CC175B9C0F1B6A831C399E269772661

  1 record(s) selected.

你可以试试这个:

HEX(CAST(HASH(BLOB('1'),0) as VARBINARY(128)))