【发布时间】:2022-01-09 22:33:51
【问题描述】:
我正在尝试将 NiceHash 中的一些数据导入 Excel,但我无法进行身份验证。 这是文档:https://www.nicehash.com/docs/
我在 GitHub 上找到了一些代码来帮助我处理 HMAC 签名 (https://github.com/VBA-tools/VBA-Web),但我遇到了关于如何准备输入字符串的问题
以下是输入示例,我有所有信息。
"4ebd366d-76f4-4400-a3b6-e51515d054d6 ⊠ 1543597115712 ⊠ 9675d0f8-1325-484b-9594-c9d6d3268890 ⊠ ⊠ da41b3bc-3d0b-4226-b7ea-aee73f94a518 ⊠ ⊠ GET ⊠ /main/api/v2/hashpower/orderBook ⊠算法=X16R&page=0&size=100"
唯一的问题是我不知道如何在它们之间添加零字节 (⊠) 字符。 如果您查看我的代码,在其中找到“端点”变量,您会发现我只添加了不同事物的值,但我缺少它们之间的零字节
有人知道怎么做吗?
这是我目前使用的代码
Sub test()
Dim endpoint As String
Dim secret As String
apiKey = Sheets("Settings").Cells(1, 2)
secret = Sheets("Settings").Cells(2, 2)
OrgID = Sheets("Settings").Cells(3, 2)
Dim xhr1: Set xhr1 = CreateObject("MSXML2.XMLHTTP")
With xhr1
.Open "GET", "https://api2.nicehash.com/api/v2/time"
.send
End With
x = xhr1.responsetext
t = Mid(x, 15, 13)
n = generateNonce()
endpoint = apiKey & t & n & OrgID & "GET" & "https://api2.nicehash.com/main/api/v2/hashpower/orderBook" & "algorithm=X16R&page=0&size=100"
hmac = HMACSHA256(endpoint, secret)
Dim xhr: Set xhr = CreateObject("MSXML2.XMLHTTP")
With xhr
.Open "GET", "https://api2.nicehash.com/main/api/v2/hashpower/orderBook?algorithm=X16R&page=0&size=100", False
.setRequestHeader "X-Time", t
.setRequestHeader "X-Nonce", n
.setRequestHeader "X-Organization-Id", OrgID
.setRequestHeader "X-Request-Id", "test"
.setRequestHeader "X-Auth", apiKey & ":" & hmac
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.setRequestHeader "Content-Type", "application/json"
End With
xhr.send
Debug.Print xhr.responsetext
End Sub
以下是 HMAC 函数
Public Function HMACSHA256(secret As String, Optional Format As String = "Hex") As String
Dim web_Crypto As Object
Dim web_TextBytes() As Byte
Dim web_SecretBytes() As Byte
Dim web_Bytes() As Byte
web_TextBytes = VBA.StrConv(Text, vbFromUnicode)
web_SecretBytes = VBA.StrConv(secret, vbFromUnicode)
Set web_Crypto = CreateObject("System.Security.Cryptography.HMACSHA256")
web_Crypto.Key = web_SecretBytes
web_Bytes = web_Crypto.ComputeHash_2(web_TextBytes)
Select Case Format
Case "Base64"
HMACSHA256 = web_AnsiBytesToBase64(web_Bytes)
Case Else
HMACSHA256 = web_AnsiBytesToHex(web_Bytes)
End Select
End Function
Private Function web_AnsiBytesToBase64(web_Bytes() As Byte)
' Use XML to convert to Base64
Dim web_XmlObj As Object
Dim web_Node As Object
Set web_XmlObj = CreateObject("MSXML2.DOMDocument")
Set web_Node = web_XmlObj.createElement("b64")
web_Node.DataType = "bin.base64"
web_Node.nodeTypedValue = web_Bytes
web_AnsiBytesToBase64 = web_Node.Text
Set web_Node = Nothing
Set web_XmlObj = Nothing
End Function
Private Function web_AnsiBytesToHex(web_Bytes() As Byte)
Dim web_i As Long
For web_i = LBound(web_Bytes) To UBound(web_Bytes)
web_AnsiBytesToHex = web_AnsiBytesToHex & VBA.LCase$(VBA.Right$("0" & VBA.Hex$(web_Bytes(web_i)), 2))
Next web_i
End Function
Function generateNonce()
Dim Nonce As String
Dim alphaNumeric As Variant
alphaNumeric = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
Randomize
For i = 1 To 32
Nonce = Nonce & alphaNumeric(61 * Rnd)
Next
generateNonce = Nonce
End Function
【问题讨论】: