我正在尝试转换C#代码(特别是" QueryPrivate"函数): https://bitbucket.org/arrivets/krakenapi/src/cff138b017c38efde2db1a080fb765790a6d04c8/KrakenClient/KrakenClient.cs?at=master&fileviewer=file-view-default
使用VB Net代码。我一直收到一个"无效的密钥"响应。以下是我提出的代码:
Private Function QueryPrivate(a_sMethod As String, Optional props As String = Nothing) As String
' generate a 64 bit nonce using a timestamp at tick resolution
_url = "https://api.kraken.com"
_version = 0
_key = "Key Here"
_secret = "Secret Here"
_rateGate = New RateGate(1, TimeSpan.FromSeconds(5))
Dim nonce As Int64 = DateTime.Now.Ticks
props = Convert.ToString("nonce=" + nonce.ToString()) & props
Dim path As String = String.Format("/{0}/private/{1}", _version, a_sMethod)
Dim address As String = _url & path
Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(address), HttpWebRequest)
webRequest__1.ContentType = "application/x-www-form-urlencoded"
webRequest__1.Method = "POST"
webRequest__1.Headers.Add("API-Key", _key)
Dim base64DecodedSecred As Byte() = Convert.FromBase64String(_secret)
Dim np = nonce.ToString() + Convert.ToChar(0) + props
Dim pathBytes = Encoding.UTF8.GetBytes(path)
Dim hash256Bytes = sha256_hash(np)
Dim z = New Byte(pathBytes.Count() + (hash256Bytes.Count() - 1)) {}
pathBytes.CopyTo(z, 0)
hash256Bytes.CopyTo(z, pathBytes.Count())
Dim signature = getHash(base64DecodedSecred, z)
webRequest__1.Headers.Add("API-Sign", Convert.ToBase64String(signature))
If props IsNot Nothing Then
Using writer = New StreamWriter(webRequest__1.GetRequestStream())
writer.Write(props)
End Using
End If
'Make the request
Try
'Wait for RateGate
_rateGate.WaitToProceed()
Using webResponse As WebResponse = webRequest__1.GetResponse()
Using str As Stream = webResponse.GetResponseStream()
Using sr As New StreamReader(str)
Dim responseContent3 As String = sr.ReadToEnd
Return responseContent3
End Using
End Using
End Using
Catch wex As WebException
Using response As HttpWebResponse = DirectCast(wex.Response, HttpWebResponse)
Using str As Stream = response.GetResponseStream()
Using sr As New StreamReader(str)
Dim responseContent3 As String = sr.ReadToEnd
Return responseContent3
End Using
End Using
End Using
End Try
End Function
Private Function sha256_hash(value As [String]) As Byte()
Using hash As SHA256 = SHA256Managed.Create()
Dim enc As Encoding = Encoding.UTF8
Dim result As [Byte]() = hash.ComputeHash(enc.GetBytes(value))
Return result
End Using
End Function
Private Function getHash(keyByte As Byte(), messageBytes As Byte()) As Byte()
Using hmacsha512 = New HMACSHA512(keyByte)
Dim result As [Byte]() = hmacsha512.ComputeHash(messageBytes)
Return result
End Using
End Function
我无法弄清楚为什么这会导致返回无效密钥,除了我对密钥进行编码的方式必须与在C#中完成的方式不同之后
答案 0 :(得分:0)
您的代码中存在多个问题,您可以将字符串和字符组合在一起。
要在VB中连接字符串,请使用&
而不是“+”
您还使用了几个帮助函数进行散列,这些函数也可能存在问题。
最好的办法是同时在C#和VB中启动程序,然后逐步执行每行代码,直到找到两者之间的差异为止。