sara moradi
sara moradi

Reputation: 43

SQL - Using HASHBYTES for Encryption password but it change to Chinese words

I am going to use HASHBYTES() for hiding my password but when I am going to insert them into a table it changes the password to Chinese words. my code:

INSERT INTO [Library].[dbo].[Supervisor]
VALUES(NEWID(), N'Abbas Jafari', '@AbbasJafari', HASHBYTES('SHA2_256', 'P@ssw0rd'))

my output:

ID Name UserName Password
C72B34EC-6FDC-467E-9E76-766570EA7A55 Abbas Jafari @AbbasJafari 봡�㼘๴濧뜧뎎誜狙垧

but when I am going to use this function in a SELECT statement it works well:

SELECT HASHBYTES('SHA2_256', 'P@ssw0rd'); --output:0xB03DDF3CA2E714A6548E7495E2A03F5E824EAAC9837CD7F159C67B90FB4B7342

Do anyone knows what is wrong with "봡�㼘๴濧뜧뎎誜狙垧"? can I leave it like this?

Upvotes: 1

Views: 658

Answers (1)

Sparky
Sparky

Reputation: 15105

Change your data type from nVARCHAR(max) to varbinary(max)

SELECT CAST(HASHBYTES('SHA2_256', 'P@ssw0rd') as nvarchar(max))
SELECT CAST(HASHBYTES('SHA2_256', 'P@ssw0rd') as varbinary(max))

or varbinary(32) if you'd prefer

Upvotes: 2

Related Questions