问题
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