How to generate MD5 using VBScript in classic ASP?

前端 未结 6 522
慢半拍i
慢半拍i 2020-12-05 11:29

I need to generate an MD5 in my application.

I\'ve tried google but only find PHP code for MD5. I need to connect to a client system that validates using MD5 hash bu

6条回答
  •  旧时难觅i
    2020-12-05 12:16

    First of all, thank you SgtWilko! :)

    Based on your collected information, I've done one function for all (not for base64/Files).
    Your code was very useful for me, but I was searching for a more PHP alike (simple) Function to deal with plain text and with a more explicit code.

    Edited:
    Based on the issue How to hash a UTF-8 string in Classic ASP, I come up with the ADODB.Stream solution. You can now use non-English characters.

    Edited:
    Parameter PlainText was changed to Target. You can now use the HMAC versions.
    Just use the Target parameter as an array.

    Target(0) = PlainText
    Target(1) = SharedKey
    

    Thank you again SgtWilko ;)

    Announcing the first SHA1 collision (Google Security Blog) February 23, 2017.

    With this function you can hash the plain text into:
    MD5, RIPEMD160, SHA1, SHA256, SHA384, SHA512, HMACMD5, HMACRIPEMD160, HMACSHA1, HMACSHA256, HMACSHA384 and HMACSHA512
    If you need more you can find it in: System.Security.Cryptography Namespace

    Function Hash(HashType, Target)
        On Error Resume Next
    
        Dim PlainText
    
        If IsArray(Target) = True Then PlainText = Target(0) Else PlainText = Target End If
    
        With CreateObject("ADODB.Stream")
            .Open
            .CharSet = "Windows-1252"
            .WriteText PlainText
            .Position = 0
            .CharSet = "UTF-8"
            PlainText = .ReadText
            .Close
        End With
    
        Set UTF8Encoding = CreateObject("System.Text.UTF8Encoding")
        Dim PlainTextToBytes, BytesToHashedBytes, HashedBytesToHex
    
        PlainTextToBytes = UTF8Encoding.GetBytes_4(PlainText)
    
        Select Case HashType
            Case "md5": Set Cryptography = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider") '< 64 (collisions found)
            Case "ripemd160": Set Cryptography = CreateObject("System.Security.Cryptography.RIPEMD160Managed")
            Case "sha1": Set Cryptography = CreateObject("System.Security.Cryptography.SHA1Managed") '< 80 (collision found)
            Case "sha256": Set Cryptography = CreateObject("System.Security.Cryptography.SHA256Managed")
            Case "sha384": Set Cryptography = CreateObject("System.Security.Cryptography.SHA384Managed")
            Case "sha512": Set Cryptography = CreateObject("System.Security.Cryptography.SHA512Managed")
            Case "md5HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACMD5")
            Case "ripemd160HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACRIPEMD160")
            Case "sha1HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA1")
            Case "sha256HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA256")
            Case "sha384HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA384")
            Case "sha512HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA512")
        End Select
    
        Cryptography.Initialize()
    
        If IsArray(Target) = True Then Cryptography.Key = UTF8Encoding.GetBytes_4(Target(1))
    
        BytesToHashedBytes = Cryptography.ComputeHash_2((PlainTextToBytes))
    
        For x = 1 To LenB(BytesToHashedBytes)
            HashedBytesToHex = HashedBytesToHex & Right("0" & Hex(AscB(MidB(BytesToHashedBytes, x, 1))), 2)
        Next
    
        If Err.Number <> 0 Then Response.Write(Err.Description) Else Hash = LCase(HashedBytesToHex)
    
        On Error GoTo 0
    End Function
    

    These can be used as follows:

    Hash("sha512", "Hello World")
    

    Produces:
    2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b

    Hash("sha256", "Hello World")
    

    Produces:
    a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

    Hash("md5", "muñeca")
    

    Produces:
    ea07bec1f37f4b56ebe368355d1c058f

    Hash("sha512HMAC", Array("Hello World", "Shared Key"))
    

    Produces:
    28e72824c48da5a5f14b59246905d2839e7c50e271fc078b1c0a75c89b6a3998746bd8b2dc1764b19d312702cf5e15b38ce799156af28b98ce08b85e4df65b32

提交回复
热议问题