使用 none 英文文本时 mysql 中等效的哈希字节
hashbytes equivalent in mysql when working with none english text
当我使用 none-英文字符时,生成的哈希值不一样,但除此之外,一切都很好。
有没有人有解决方案来获得相同的结果?
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
根据 MySQL 从 SELECT HEX(CAST('سلام' as binary))
返回 D8B3D984D8A7D985
看来 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;
哪个returns:
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;
哪个returns:
UTF16-Hash
0x0381CA5081FBC68B2F55F2F2C21399D7
UTF8-Hash
0x78903C575B0DDA53C4A7644A2DD36D0E
希望对您有所帮助!
当我使用 none-英文字符时,生成的哈希值不一样,但除此之外,一切都很好。 有没有人有解决方案来获得相同的结果?
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
根据 MySQL 从 SELECT HEX(CAST('سلام' as binary))
返回 D8B3D984D8A7D985
看来 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;
哪个returns:
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;
哪个returns:
UTF16-Hash
0x0381CA5081FBC68B2F55F2F2C21399D7
UTF8-Hash
0x78903C575B0DDA53C4A7644A2DD36D0E
希望对您有所帮助!