SHA1 Hash creation returning different results in SQL Server

HI all,
If I create a HASH on SQL Server using two different methods and get two different results and I can't figure out why.  One is correct based on a similar function I am using in VB.net; so I know that the 2nd SQL function is correct, at least the result i'm accpecting.

'First Code (Not correct)
DECLARE @SomeValue nvarchar(50)
SELECT @SomeValue = 'ME@SomeValue'
SELECT HashBytes('SHA1', @SomeValue)
GO

'Result = 0x05B922B8F204CDA36731DC3078563D7E18D27052

'------------------------------------------------------------------------------
'2nd Code (Correct)
SELECT HashBytes('SHA1', 'ME@SomeValue')

'Result = 0xE45FD18F98479679C7B04122F4B7C6D3D6962463
LVL 4
Thunder724Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
In the first example you are passing a unicode string. In the second it's an ASCII string. If you modify your first example as follows you'll get the same result both times:

DECLARE @SomeValue varchar(50)
SELECT @SomeValue = 'ME@SomeValue'
SELECT HashBytes('SHA1', @SomeValue)

Open in new window


Alternately, if you modify your second example as follows you'll be passing a unicode string and you'll get the same response as the first example.

SELECT HashBytes('SHA1', N'ME@SomeValue')

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thunder724Author Commented:
Thanks but in the first example you are giving me it looks like the same code.
Am I not seeing something here?

thanks,
John.
0
Russ SuterCommented:
You declared the variable as "nvarchar(50)". I declared it as "varchar(50)". Note that I didn't include the "n" at the beginning. This specifies an ASCII string rather than a unicode string.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Thunder724, Russ Suter's right.
If you check it better you'll see that they are different data types.

In your example is a Nvarchar(50) and in his example it's a Varchar(50). That 'N' makes all the difference between being Unicode or not.
0
Thunder724Author Commented:
Oh yes.. Good grief that was driving me crazy.  I think I need new glasses.
Thanks very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.