Handling hashed passwords stored as varbinary in SQL Server and classic ASP

亡梦爱人 提交于 2019-12-04 17:21:33

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>
CJM

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,

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?

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