如何在经典ASP中使用VBScript生成MD5?

use*_*384 12 vbscript md5 asp-classic md5sum

我需要在我的应用程序中生成MD5.

我试过谷歌但只找到MD5的PHP代码.我需要连接到使用MD5哈希验证的客户端系统,但是他们的代码是PHP,我的是使用VBScript的Classic ASP.

我的服务器是.Net支持的,所以我不能使用PHP脚本.Classic ASP中的VBScript是否有这样的MD5代码?

Sgt*_*lko 33

更新2017-02-21 - 现在为JWT添加了HMACSHA256

更新2016-07-05 - 现在添加了SHA1和SHA256

是的,对于那些一直在努力解决这个问题的人(比如我自己)而且想知道,这是可能的!

以下代码分为几个函数,以便您可以将MD5/sha1/sha256作为字符串或文件.

我从另一个stackexchange借用了函数GetBytes和BytesToBase64,stringToUTFBytes中的代码基于另一个stackexchange.

function md5hashBytes(aBytes)
    Dim MD5
    set MD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

    MD5.Initialize()
    'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
    md5hashBytes = MD5.ComputeHash_2( (aBytes) )
end function

function sha1hashBytes(aBytes)
    Dim sha1
    set sha1 = CreateObject("System.Security.Cryptography.SHA1Managed")

    sha1.Initialize()
    'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
    sha1hashBytes = sha1.ComputeHash_2( (aBytes) )
end function

function sha256hashBytes(aBytes)
    Dim sha256
    set sha256 = CreateObject("System.Security.Cryptography.SHA256Managed")

    sha256.Initialize()
    'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
    sha256hashBytes = sha256.ComputeHash_2( (aBytes) )
end function

function sha256HMACBytes(aBytes, aKey)
    Dim sha256
    set sha256 = CreateObject("System.Security.Cryptography.HMACSHA256")

    sha256.Initialize()
    sha256.key=aKey
    'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
    sha256HMACBytes = sha256.ComputeHash_2( (aBytes) )
end function

function stringToUTFBytes(aString)
    Dim UTF8
    Set UTF8 = CreateObject("System.Text.UTF8Encoding")
    stringToUTFBytes = UTF8.GetBytes_4(aString)
end function

function bytesToHex(aBytes)
    dim hexStr, x
    for x=1 to lenb(aBytes)
        hexStr= hex(ascb(midb( (aBytes),x,1)))
        if len(hexStr)=1 then hexStr="0" & hexStr
        bytesToHex=bytesToHex & hexStr
    next
end function

Function BytesToBase64(varBytes)
    With CreateObject("MSXML2.DomDocument").CreateElement("b64")
        .dataType = "bin.base64"
        .nodeTypedValue = varBytes
        BytesToBase64 = .Text
    End With
End Function

'Special version that produces the URLEncoded variant of Base64 used in JWTs.
Function BytesToBase64UrlEncode(varBytes)
    With CreateObject("MSXML2.DomDocument").CreateElement("b64")
        .dataType = "bin.base64"
        .nodeTypedValue = varBytes
        BytesToBase64UrlEncode = replace(replace(replace(replace(replace(.Text,chr(13),""),chr(10),""),"+", "-"),"/", "_"),"=", "")
    End With
End Function

Function GetBytes(sPath)
    With CreateObject("Adodb.Stream")
        .Type = 1 ' adTypeBinary
        .Open
        .LoadFromFile sPath
        .Position = 0
        GetBytes = .Read
        .Close
    End With
End Function
Run Code Online (Sandbox Code Playgroud)

这些可以使用如下:

BytesToBase64(md5hashBytes(stringToUTFBytes("Hello World")))
Run Code Online (Sandbox Code Playgroud)

产生:sQqNsWTgdUEFt6mb5y4/5Q ==

bytesToHex(md5hashBytes(stringToUTFBytes("Hello World")))
Run Code Online (Sandbox Code Playgroud)

产品:B10A8DB164E0754105B7A99BE72E3FE5

对于SHA1:

bytesToHex(sha1hashBytes(stringToUTFBytes("Hello World")))
Run Code Online (Sandbox Code Playgroud)

产品:0A4D55A8D778E5022FAB701977C5D840BBC486D0

对于SHA256:

bytesToHex(sha256hashBytes(stringToUTFBytes("Hello World")))
Run Code Online (Sandbox Code Playgroud)

产品:A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E

获取文件的MD5(对Amazon S3 MD5检查很有用):

BytesToBase64(md5hashBytes(GetBytes(sPath)))
Run Code Online (Sandbox Code Playgroud)

其中sPath是本地文件的路径.

最后,创建一个JWT:

'define the JWT header, needs to be converted to UTF bytes:
aHead=stringToUTFBytes("{""alg"":""HS256"",""typ"":""JWT""}")

'define the JWT payload, again needs to be converted to UTF Bytes.
aPayload=stringToUTFBytes("{""sub"":""1234567890"",""name"":""John Doe"",""admin"":true}") 

'Your shared key.
theKey="mySuperSecret"

aSigSource=stringToUTFBytes(BytesToBase64UrlEncode(aHead) & "." & BytesToBase64UrlEncode(aPayload))

'The full JWT correctly Base 64 URL encoded.
aJWT=BytesToBase64UrlEncode(aHead) & "." & BytesToBase64UrlEncode(aPayload) & "." & BytesToBase64UrlEncode(sha256HMACBytes(aSigSource,stringToUTFBytes(theKey)))
Run Code Online (Sandbox Code Playgroud)

这将生成以下有效的JWT:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.7ofvtkn0z_pTl6WcqRTxw-4eSE3NqcEq9_3ax0YcuIQ

  • 到目前为止的最佳答案...正是我正在寻找的,除了我使用bytesToHex(md5hashBytes(GetBytes(sPath)))从文件中获取我需要的值.其他工作的优秀编译先生和最好的部分是没有所需的杂项可执行文件或库.MD5与sigcheck和其他misc可执行实用程序对齐. (3认同)

use*_*384 5

感谢上面提供的所有链接,它们很有用,但是如果有人需要它,我发现这个确实很有用. VBScript中-MD5


小智 5

这是一个可读且可下载的MD5版本作为VBS脚本:

https://github.com/Wikinaut/md5.vbs

这是来自http://chayoung.tistory.com/entry/VBScript-MD5的代码(感谢您提供这段独特的代码).


cam*_*inc 4

我不知道这段代码是否有效,因为我无法测试它。然而,这似乎就是你所要求的。

http://www.bullzip.com/md5/vb/md5-vb-class.htm

这是 Jeff Attwood 撰写的关于哈希的有趣文章。关于 MD5,他有一些重要的事情要说:

http://www.codinghorror.com/blog/2012/04/speed-hashing.html

  • 这甚至不是 [tag:vbscript] 的实现,是针对 [tag:vb] 的。 (2认同)