处理在 SQL Server 和经典 ASP 中存储为 varbinary 的哈希密码

发布于 2024-07-17 20:08:17 字数 774 浏览 4 评论 0原文

各位,

提前抱歉 - 对于以下大多数主题(SQL、ASP),我都是新手。 无论如何......

我有一个非常简单的网络应用程序,要求用户使用用户名和密码登录。

前端创建密码的加盐 SHA1 哈希值,并将其(与用户名一起)发布到 ASP 页面。

该 ASP 页获取数据,调用 SQL Server 数据库中的存储过程,并传递用户名和散列密码; 存储过程将信息写入“用户”表。

表中密码列的类型是varbinary。

据我所知,当 ASP 获取密码(password = Request.Form("password"))时,它是一个字符串。

如果我这样创建查询,我可以“欺骗”SQL Server 将其作为 varbinary 处理:

query = "EXEC sp_save_user @username='" & 用户名和 "', @password=0x" & 密码

IOW - 我在密码字符串前面加上“0x”。

但是,我读到使用参数化查询是更好的做法:

例如: SET objParam = objCommand.CreateParameter("@password",204, 1, 40, password)

但是,这会失败,因为参数应该是二进制(204),但密码是字符串。

那么,如何将“4a5e6a8d521ed487b81c91e131cf27e8dae9b783”这样的字符串转换为 ASP 中的二进制文件?

提前谢谢了!

All,

Sorry in advance - I'm a novice in most of the topics below (SQL, ASP). Anyway...

I've got a pretty simple web app that requires users to log in with a user name and password.

The front end creates a salted SHA1 hash of the password, and posts it (along with the user's name) to an ASP page.

That ASP page takes the data, calls a stored procedure in the SQL Server database, and passes the users name and hashed password; the stored procedure writes the info to the 'users' table.

The password column's type in the table is varbinary.

As far as I can tell, when the ASP gets the password (password = Request.Form("password")), it's a String.

I can 'trick' SQL Server into handling it as a varbinary if I create the query this way:

query = "EXEC sp_save_user @username='" & username & "', @password=0x" & password

IOW - I'm prepending an "0x" to the password string.

However, I've read that it's a better practice to use a parameterized query:

E.g.: SET objParam = objCommand.CreateParameter("@password",204, 1, 40, password)

However, this fails because the parameter is supposed to be binary (204), but password is a string.

So, how do I convert a string like "4a5e6a8d521ed487b81c91e131cf27e8dae9b783" to a binary in ASP?

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑咖 2024-07-24 20:08:17

我记得那些日子我常常为这种事情敲头。 我建议您升级到 ASP.Net,但同时以下代码 (VBScript) 应该可以满足您的需求:

<%

Dim result

main()

function main()

    Dim userName : userName = "Martin"
    Dim data : data = "4a5e6a8d521ed487b81c91e131cf27e8dae9b783"

    Dim db: Set db = Server.CreateObject("ADODB.Connection")
    db.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=password1;Initial Catalog=Test;Data Source=(local)"

    Dim cmd : Set cmd = Server.CreateObject("ADODB.Command")
    cmd.ActiveConnection = db

    cmd.CommandText = "dbo.[sp_save_user]"
    cmd.CommandType = 4    
    cmd.Parameters.Append cmd.CreateParameter("@UserName", 200, 1, 50, userName)
    Dim bytes : bytes = stringToBinary(data)
    cmd.Parameters.Append cmd.CreateParameter("@Password", 204, 1, LenB(bytes), bytes)
    cmd.Execute()

    db.Close

    result = "done"

end function

function stringToBinary(str)
    dim ahex
    for i=0 to len(str) - 1 step 2
        Dim strDigit1 
        Dim strDigit2
        strDigit1 = Ucase(Mid(str, i+1, 1))
        strDigit2 = Ucase(Mid(str, i+2, 1))

        Dim byteDigit1
        Dim byteDigit2
        byteDigit1 = InStr("0123456789ABCDEF", strDigit1) - 1
        byteDigit2 = InStr("0123456789ABCDEF", strDigit2) - 1

        ahex = ahex & ChrB((byteDigit1 * 16) + byteDigit2)
    next   

    stringToBinary = ahex          
end function
%>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <h1><%= result %></h1>
    </body>
</html>

I remember the days when I used to bash my head on this kind of thing. I suggest you get an upgrade to ASP.Net, but in the mean time the following code (VBScript) should do what you want:

<%

Dim result

main()

function main()

    Dim userName : userName = "Martin"
    Dim data : data = "4a5e6a8d521ed487b81c91e131cf27e8dae9b783"

    Dim db: Set db = Server.CreateObject("ADODB.Connection")
    db.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=password1;Initial Catalog=Test;Data Source=(local)"

    Dim cmd : Set cmd = Server.CreateObject("ADODB.Command")
    cmd.ActiveConnection = db

    cmd.CommandText = "dbo.[sp_save_user]"
    cmd.CommandType = 4    
    cmd.Parameters.Append cmd.CreateParameter("@UserName", 200, 1, 50, userName)
    Dim bytes : bytes = stringToBinary(data)
    cmd.Parameters.Append cmd.CreateParameter("@Password", 204, 1, LenB(bytes), bytes)
    cmd.Execute()

    db.Close

    result = "done"

end function

function stringToBinary(str)
    dim ahex
    for i=0 to len(str) - 1 step 2
        Dim strDigit1 
        Dim strDigit2
        strDigit1 = Ucase(Mid(str, i+1, 1))
        strDigit2 = Ucase(Mid(str, i+2, 1))

        Dim byteDigit1
        Dim byteDigit2
        byteDigit1 = InStr("0123456789ABCDEF", strDigit1) - 1
        byteDigit2 = InStr("0123456789ABCDEF", strDigit2) - 1

        ahex = ahex & ChrB((byteDigit1 * 16) + byteDigit2)
    next   

    stringToBinary = ahex          
end function
%>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <h1><%= result %></h1>
    </body>
</html>
爱,才寂寞 2024-07-24 20:08:17

我假设您有范围更改数据库...更具体地说是存储过程?

为什么不直接接受散列作为字符串并将其 CAST 到 SProc 中的 varbinary ?

进一步阅读,没有内置 String -> SQL Server 中的 Varbinary 函数,但此函数作为一个简单的解决方案提供,

I assume you have scope to change the DB... more specifically the Stored Procedure?

Why not just accept the hash as a string and CAST it to varbinary within the SProc?

Reading further, there is no built-in String -> Varbinary function in SQL Server, but this function has been offered as an easy solution,

画▽骨i 2024-07-24 20:08:17

也许有点不相关,但我想知道,如果您在客户端上对哈希加盐,那么您到底如何保证盐的安全?

Maybe a bit unrelated, but I'm wondering, if you are salting the hash on the client, how are you exactly keeping the salt secure?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文