1

I have the special character ™ that I am trying to convert to a MD5 hash. The expected hash result should be dbe87324ccfa4dfda26dad01688b3232. The results I am getting are unexpected to say the least. The below queries tries to yield results for both VARCHAR and NVARCHAR text.

SELECT CONVERT(VARCHAR(32), HASHBYTES('MD5', N'™'), 2) -- 254C54912C4A445697823E7D1AC70EA2
SELECT CONVERT(VARCHAR(32), HASHBYTES('MD5', '™'), 2) -- 6D4A60F6F35FB2CEDA4DF4FE58B2C9BE
Dale K
  • 25,246
  • 15
  • 42
  • 71
Mike
  • 826
  • 11
  • 31
  • 3
    From where are you getting the expected value? Specifically, what is the encoding of the text that you're running through md5 to get that result? IIRC, SQL Server is UTF-16 little-endian, so if your source encoding is something else, the bytes will be different yielding a different hash. – Ben Thul Aug 07 '19 at 22:45
  • I got the expected value from here a couple sites (http://www.miraclesalad.com/webtools/md5.php) and (https://www.md5hashgenerator.com/) and also from comparing Java generated MD5 hash using the same character. – Mike Aug 08 '19 at 01:36
  • 2
    Sure. But the encoding matters. See my previous comment. – Ben Thul Aug 08 '19 at 02:46
  • Sorry, yes, the character encoding if UTF-8, based on this site (https://dencode.com/en/hash) UTF-8 encoding would yield the correct value as well. Thanks for pointing me to the correct direction, looks like I would need to do some sort of conversion to get the correct encoding to hash (https://stackoverflow.com/questions/35200452/compute-md5-hash-of-a-utf8-string) – Mike Aug 08 '19 at 03:05

0 Answers0