gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:00:24 29 4
gpt4 key购买 nike

我需要在我的应用程序中生成一个 MD5。

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

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

最佳答案

更新 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

这些可以按如下方式使用:
BytesToBase64(md5hashBytes(stringToUTFBytes("Hello World")))

产生:sQqNsWTgdUEFt6mb5y4/5Q==
bytesToHex(md5hashBytes(stringToUTFBytes("Hello World")))

生产:B10A8DB164E0754105B7A99BE72E3FE5

对于 SHA1:
bytesToHex(sha1hashBytes(stringToUTFBytes("Hello World")))

生产:0A4D55A8D778E5022FAB701977C5D840BBC486D0

对于 SHA256:
bytesToHex(sha256hashBytes(stringToUTFBytes("Hello World")))

生产:A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E

获取文件的 MD5(对于 Amazon S3 MD5 检查很有用):
BytesToBase64(md5hashBytes(GetBytes(sPath)))

其中 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)))

这将产生以下有效的 JWT:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.7ofvtkn0xlq3Eq3EqsE

关于asp-classic - 如何在经典 ASP 中使用 VBScript 生成 MD5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10198690/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com