How to hash a UTF-8 string in Classic ASP

后端 未结 6 1965
终归单人心
终归单人心 2021-01-15 19:33

I\'ve been looking for a Classic ASP script that allows me to hash a string using the MD5 algorithm. I need to match this hashed string against the same string in an ASP.NET

6条回答
  •  -上瘾入骨i
    2021-01-15 19:56

    If you are using UTF-8 on both .Net and Classic ASP you should be able to borrow the .Net implementation.

    Something like this;

    <%
    Dim asc, enc, bytes, instr, outstr, pos
    
    instr = "muñeca"
    
    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    
    bytes = asc.GetBytes_4(instr)
    bytes = enc.ComputeHash_2((bytes))
    
    outstr = ""
    
    'Convert the Byte Array to a Hex string
    For pos = 1 To LenB(bytes)
     outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
    Next
    
    Response.Write outstr
    %>
    

    Output:

    ea07bec1f37f4b56ebe368355d1c058f
    

    This is the only valid method I've found for Classic ASP because all other implementations assume ANSI encoding and you end up with an incorrect hash.

    Credit to: Replicating PHP’s sha1() in VBScript for helping me finally find a solution.

提交回复
热议问题