TSQL md5 hash different to C# .NET md5

前端 未结 4 872
旧时难觅i
旧时难觅i 2020-11-29 09:15

I\'ve generated an md5 hash as below:

DECLARE @varchar varchar(400) 

SET @varchar = \'è\'

SELECT CONVERT(VARCHAR(2000), HASHBYTES( \'MD5\', @varchar ), 2)
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 10:07

    SQL Server uses UCS-2 rather than UTF-8 to encode character data.

    If you were using an NVarChar field, the following would work:

    System.Text.Encoding.Unicode.GetBytes("è"); // Updated per @srutzky's comments
    

    For more information on SQL and C# hashing, see

    http://weblogs.sqlteam.com/mladenp/archive/2009/04/28/Comparing-SQL-Server-HASHBYTES-function-and-.Net-hashing.aspx

提交回复
热议问题