预准备语句中的SQL Server sha1值提供的值不同于硬编码的字符串 [英] SQL Server sha1 value in prepared statement gives a different value than hardcoded string
问题描述
我正在尝试在SQL Server中加密密码,当我使用字符串与使用预准备的语句参数时,得到两个不同的结果.
I am trying to encrypt a password in SQL Server and I'm getting two different results when I use a string vs. using a prepared statement parameter.
例如:
SELECT
sys.fn_varbintohexstr(HASHBYTES('sha1', ?)),
sys.fn_varbintohexstr(HASHBYTES('sha1', 'password'))
哪里?由密码"填充.它给了我
Where the ? is populated by 'password'. It gives me
0xe8f97fba9104d1ea50479...
0x5baa61e4c9b93f3f06822...
为什么对于同一件事我会得到两个不同的结果?
Why am I getting two different results for what should be the same thing?
此外,这仅发生在SQL Server中,如果我在MySQL中执行类似的查询,则两者返回相同的值.
Also, this is only happening in SQL Server, if I do a similar query in MySQL, it returns the same value for both.
我知道我应该使用更好的加密方式,但现在我被困在sha1(没有盐)上.
I know I should be using better encryption, but I am stuck with sha1 (no salt) for now.
谢谢
推荐答案
一个是varchar,另一个是varchar
One is a varchar the other nvarchar
SELECT
sys.fn_varbintohexstr(HASHBYTES('sha1','password')),
sys.fn_varbintohexstr(HASHBYTES('sha1',N'password'))
返回
0x5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
0xe8f97fba9104d1ea5047948e6dfb67facd9f5b73
这篇关于预准备语句中的SQL Server sha1值提供的值不同于硬编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!