Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from Hashbytes function's SHA2_256 encryption datatype to varchar(256) in SQL Server 2012

Tags:

sql

sql-server

I want to implement a password encryption system using SQL in SQL Server 2012.

The HASHBYTES method must have only nvarchar, varbinary datatype.

update members 
set passwd = cast(HASHBYTES('SHA2_256', 'asd123123') as varchar(256)),
    pwchangedat = GETDATE()
where userid = 'abcd1234'

The unexpected result of the Select query applied above update query:

?lZX?갅4磎?닽뙷?I?#?뚭?

I want to fix this varchar(256) data type string 'asd123123' applied SHA-256 encryption.

I want:

c82a6c7f5a58a915814334cda5a2ef88ab8cba922f49ec1623e2248ceabf7ddc

How can I get a solution?

Regards,

like image 469
hsw4840 Avatar asked Nov 16 '17 19:11

hsw4840


1 Answers

I think this is what you are looking for...

select convert(varchar(256),HASHBYTES('SHA2_256','asd123123'),2)

See the Binary Styles Section (since Hashbytes returns varbinary)

Thus....

update members set
    passwd = convert(varchar(256),HASHBYTES('SHA2_256','asd123123'),2),
    pwchangedat = GETDATE()
    where userid = 'abcd1234'
like image 66
S3S Avatar answered Nov 14 '22 21:11

S3S