TSQL md5 hash different to C# .NET md5

前端 未结 4 879
旧时难觅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

    I was having the same issue, and as @srutzky comments, what might be happening is that I didn't preceed the query with a capital-N, and I was getting an 8-bit Extended ASCII ( VARCHAR / string not prefixed with capital-N ) instead of a 16-bit UTF-16 Little Endian ( NVARCHAR / string prefixed with capital-N )

    {Id, UserName, PasswordString, PasswordHashed}
    

    If you do:

    SELECT TOP 1 CONVERT(char(32),HashBytes('MD5', 'abc123'),2) FROM [Users]
    

    It will output: E99A18C428CB38D5F260853678922E03

    But if you do this, having the same password ('abc123'):

    SELECT CONVERT(char(32),HashBytes('MD5', [PasswordString]),2) FROM [Users]
    

    It will output: 6E9B3A7620AAF77F362775150977EEB8

    What I should have done is:

    SELECT CONVERT(char(32),HashBytes('MD5', N'abc123'),2) FROM [Users]
    

    That outputs the same result: 6E9B3A7620AAF77F362775150977EEB8

提交回复
热议问题