0

当我使用非英文字符时,生成的哈希值不一样,但除此之外,一切都很好。有没有人有解决方案来获得相同的结果?

MYSQL

  SELECT  MD5( 'سلام')
------------- result: 78903c575b0dda53c4a7644a2dd36d0e

SQL 服务器

SELECT CONVERT(VARCHAR(50), HASHBYTES('MD5',  'سلام'), 2) 
-------------- result:3C50F6458899B3C0988BE358290F5F24


SELECT CONVERT(nVARCHAR(50), HASHBYTES('MD5',  N'سلام'), 2) 
-------------- result:0381CA5081FBC68B2F55F2F2C21399D7

4

1 回答 1

2

根据 MySQL 返回D8B3D984D8A7D985SELECT HEX(CAST('سلام' as binary))结果,MySQL 似乎正在使用 UTF-8 字符编码。

在 SQL Server 2019 上,您可以使用 LATIN1_GENERAL_100_CI_AS_SC_UTF8 排序规则(SQL Server 2017 不支持),如下所示:

create table #Test (
  UTF16 nvarchar(max),
  UTF8 varchar(max)  COLLATE LATIN1_GENERAL_100_CI_AS_SC_UTF8
)
insert #Test values (N'سلام', N'سلام');

select UTF16 from #Test;
select CAST(UTF16 as varbinary) as [UTF16-Bytes] from #Test;
select UTF8 from #Test;
select CAST(UTF8 as varbinary) as [UTF8-Bytes] from #Test;

返回:

UTF16
سلام

UTF16-Bytes
0x3306440627064506

UTF8
سلام

UTF8-Bytes
0xD8B3D984D8A7D985

然后使用 hashbytes():

select hashbytes('MD5', cast(UTF16 as varbinary)) as [UTF16-Hash] from #Test;
select hashbytes('MD5', cast(UTF8 as varbinary)) as [UTF8-Hash] from #Test;

返回:

UTF16-Hash
0x0381CA5081FBC68B2F55F2F2C21399D7

UTF8-Hash
0x78903C575B0DDA53C4A7644A2DD36D0E

希望这可以帮助!

于 2020-05-15T14:20:54.073 回答