开发者

HASHBYTES of varchar returning incorrect hash value

开发者 https://www.devze.com 2023-01-12 00:49 出处:网络
I currently have a database schema that contains a user\'s password in plain text.I\'ve added a new column called password of type binary(16) with the intent of hashing the current plain text password

I currently have a database schema that contains a user's password in plain text. I've added a new column called password of type binary(16) with the intent of hashing the current plain text password via MD5. When I do this, I'm finding that the value stored in the password field is wrong. Here's my conversion query:

UPDATE my_table SET password=HASHBYTES('MD5', plain_text_password);

For one of my records, the plain text password is asdf. The correct MD5 value of this is 0x912ec803b2ce49e4a541068d495ab570. However, the record is being updated to 0xEC81AFD2DF2BDA47850F9182F4AC300D instead.

Has anyone every seen issues like this before? I'm using SQL Server 2008.

Update: Thinking about this a little more, I converted the plain text password field from varchar(MAX) to varchar(50). It displays the same way within SQL management studio, but I'm wondering if the underlying encoding from when the data was in varchar(MAX) format somehow got copied over to the new varchar(50) format, causing the 开发者_如何转开发discrepancy.


So I figured out what was going wrong here. After I converted all the plain-text password fields (or perhaps this was true all along, I'm not sure) a bunch of \0's were appended to the end of the field. So instead of the word 'apple', it was 'apple\0\0\0\0\0'. SQL Management studio doesn't show these \0's, but the Visual Studio debugger did. After removing all the trailing \0's, my problem goes away.

0

精彩评论

暂无评论...
验证码 换一张
取 消