SQL哈希密码

时间:2017-01-25 13:36:35

标签: sql-server hash asp.net-mvc-5

我使用SQL语句手动将旧数据导入MSSQL。 当我从原始文本密码哈希到数据库中的MVC 5 passwordHASH字段时。我得到了有趣的字符。我如何将哈希插入mvc5身份表?MVC 5表也使用nvarchar

select HASHBYTES('SHA2_512', 'P@ssw0rd')as beforehash --looks good in result
0x6BFCC4026B5F162799A6DC8305C09DB9C1674AC616BD5C7422A45FBB6D0816AC163047C47A1F426F4F4C6B5B5042C671EABC4FDC7310FD5B183EEF59DC274604

declare @afterhash nvarchar(128)
set @afterhash=HASHBYTES('SHA2_512', 'P@ssw0rd')
select @afterhash as afterhash --funny character if put inside a variable or insert to data base

ﱫ˄彫✖ꚙ菜쀅릝柁왊봖瑜ꐢ뭟࡭갖〖쑇ὺ潂䱏孫䉐燆볪�ၳ寽㸘姯⟜ц

1 个答案:

答案 0 :(得分:4)

hashbytes会返回varbinary数据类型,您尝试将其插入到nvarchar变量中。

如果您不想将此值存储为二进制文件但想要查看当前可以看到的文本,则根据是否要保留0x二进制前缀,您有几个选项:< / p>

declare @afterhash varbinary(500) = HASHBYTES('SHA2_512', 'P@ssw0rd')

select @afterhash as varbinaryValue
        ,convert(nvarchar(1000), @afterhash, 1) as nvarcharIncludePrefix
        ,convert(nvarchar(1000), @afterhash, 2) as nvarcharExcludePrefix

输出:

varbinaryValue         - 0x6BFCC4026B5F162799A6DC8305C09DB9C1674AC616BD5C7422A45FBB6D0816AC163047C47A1F426F4F4C6B5B5042C671EABC4FDC7310FD5B183EEF59DC274604
nvarcharIncludePrefix  - 0x6BFCC4026B5F162799A6DC8305C09DB9C1674AC616BD5C7422A45FBB6D0816AC163047C47A1F426F4F4C6B5B5042C671EABC4FDC7310FD5B183EEF59DC274604
nvarcharExcludePrefix  - 6BFCC4026B5F162799A6DC8305C09DB9C1674AC616BD5C7422A45FBB6D0816AC163047C47A1F426F4F4C6B5B5042C671EABC4FDC7310FD5B183EEF59DC274604