SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Hashbytes function returning unexpected results


Hashbytes function returning unexpected results

Author
Message
timothy.a.gitchel
timothy.a.gitchel
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 28
I have a script that updates a user table and sets the passwords to a hash value using the Hashbytes function:

update secUser
set [password] = HashBytes('MD5', 'PW4' + [UserName]),
HasChangedPassword = 0
GO

This is producing unexpected results in that if I run the Hashbyte function for a specific user value, such as:

select HashBytes('MD5', 'PW4avenue')

I get a result of '0x8847665608F36E2534A655D3EB57BC0F', but the table updated from the above statement has '0x1E0F528E3CD45F3D9BEFBFFAB073FE41' in it.

I can't figure out where this value is coming from.

Running this query:

select HashBytes('MD5', 'PW4' + [UserName]) as [password],
'PW4' + [UserName] as test from secUser
where [UserName] = 'avenue'

returns these results:

avenue 0x1E0F528E3CD45F3D9BEFBFFAB073FE41 PW4avenue

Any ideas what is going on here?
Christopher Stobbs
Christopher Stobbs
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2408 Visits: 2233
Hi there,

I'm not sure how Hasbytes works but I went on a hunch and here is my code


DECLARE @tbl TABLE
(UserName1 VARCHAR(100),
UserName2 NVARCHAR(100))
INSERT INTO @tbl
SELECT 'avenue','avenue'

--VARCHAR RESULTS
select HashBytes('MD5', 'PW4' + 'avenue') as [password],
'PW4' + 'avenue' as test

select HashBytes('MD5', 'PW4' + UserName1) as [password],
'PW4' + UserName1 as test
FROM @tbl


--NVARCHAR RESULTS
select HashBytes('MD5', 'PW4' + N'avenue') as [password],
'PW4' + N'avenue' as test

select HashBytes('MD5', 'PW4' + UserName2) as [password],
'PW4' + UserName2 as test
FROM @tbl




It seems that your table is storing the user name as a NVarchar

however when you run it normally you using a varchar, so the results are different

----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley

w00t
Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
timothy.a.gitchel
timothy.a.gitchel
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 28
Thanks for the assistance. I changed my update statement to the following:

update secUser
set [password] = HashBytes('MD5', 'PW4' + cast([UserName] as varchar(100))),
HasChangedPassword = 0
GO

This gives me the original values I'm expecting since the code that checks the values is using a varchar.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search