Hashing password into SQL

二次信任 提交于 2019-12-11 06:49:13

问题


I'm hashing a password in SQL directly like this :

DECLARE @HashThis nvarchar(4000);
SET @HashThis = 'SecretPizza'

INSERT into Users (UserId,Password)
Values ('CryptTest',HASHBYTES('SHA1', @HashThis))

Result :

When I try and change the SHA1 Algorithm to SHA2_256 or SHA2_512 I get the following error :

Question 1 - Is this really supposed to give me chinese like characters ?
Question 2 - Those are valid algorithm so how come I can't use them and why is encryption setting @HashThis to null?


回答1:


Question 1: You get "Chinese like" characters because you are inserting a varbinary value returned by HASHBYTES into an nvarchar column, so SQL Server is trying to interpret the bytes as Unicode code points (characters).

Question 2: Not supported before SQL Server 2012 - see SQL Server 2008 R2 HASHBYTES SHA2 returns null




回答2:


first do

select HASHBYTES('SHA2-256', @HashThis) from dual;

and see what you get..



来源:https://stackoverflow.com/questions/11691253/hashing-password-into-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!