VBA zero byte/ NiceHash Authentification /HMAC SHA256

I am trying to get some data from NiceHash into Excel but I cannot authenticate. Here is the documentation: https://www.nicehash.com/docs/

I have found some code on GitHub to help me with HMAC signature (https://github.com/VBA-tools/VBA-Web) but I got an issue on how to prepare the input string

Below is an example of input and I have all the info.

"4ebd366d-76f4-4400-a3b6-e51515d054d6 ⊠ 1543597115712 ⊠ 9675d0f8-1325-484b-9594-c9d6d3268890 ⊠ ⊠ da41b3bc-3d0b-4226-b7ea-aee73f94a518 ⊠ ⊠ GET ⊠ /main/api/v2/hashpower/orderBook ⊠ algorithm=X16R&page=0&size=100"

The only issue is that I don't know how to add zero byte (⊠) characters between. If you look into my code where you find the "endpoint" variable you see that I have added only the values of different things but I am missing the zero byte between them

Does anyone know how to do it?

This is the code I use so far

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"
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

Debug.Print xhr.responsetext
End Sub

Below are the HMAC Functions

    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")
For i = 1 To 32
    Nonce = Nonce & alphaNumeric(61 * Rnd)
generateNonce = Nonce
End Function

Read more here: https://stackoverflow.com/questions/66282091/vba-zero-byte-nicehash-authentification-hmac-sha256

Content Attribution

This content was originally published by Nacho at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: