微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

VBA 零字节/NiceHash 认证/HMAC SHA256

如何解决VBA 零字节/NiceHash 认证/HMAC SHA256

我正在尝试从 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⊠/主/ API / V2 / hashpower /手持订单⊠ 算法=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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。