Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.Collections.Generic
- Imports System.IO
- Imports System.Net
- Imports System.Security.Cryptography
- Imports System.Text
- Class Test
- Public Shared Function Main() As Integer
- Dim auth As APIAuth = New APIAuth("key", "secret")
- Dim client As APIClient = New APIClient("https://business-sandbox.cryptopay.me", auth)
- Dim path As String = "/api/invoices"
- Dim data As String = "{""price_amount"":""0.5"",""price_currency"":""ETH"",""pay_currency"":""ETH""}"
- Dim response As String = client.DoRequest(DateTime.Now, path, data)
- Console.WriteLine(response)
- Return 0
- End Function
- End Class
- Class APIClient
- Private baseURL As String
- Private auth As APIAuth
- Public Sub New(ByVal url As String, ByVal apiAuth As APIAuth)
- baseURL = url
- auth = apiAuth
- End Sub
- Public Function DoRequest(ByVal date As DateTime, ByVal path As String, ByVal Optional data As String = "", ByVal Optional contentType As String = "application/json", ByVal Optional Headers As List(Of KeyValuePair(Of String, String)) = Nothing, ByVal Optional verb As String = "POST") As String
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
- Dim request As HttpWebRequest = CType(WebRequest.Create(baseURL & path), HttpWebRequest)
- Dim response As HttpWebResponse
- Dim sw As StreamWriter
- Dim sr As StreamReader
- Dim dateStr As String
- Dim responseData As String
- request.Method = verb
- request.ContentType = contentType
- request.ContentLength = data.Length
- request.Date = date
- dateStr = date.ToUniversalTime().ToString("r")
- request.Headers.Add("Authorization", auth.GenerateAuthorizationHeader(verb, data, contentType, dateStr, path))
- sw = New StreamWriter(request.GetRequestStream())
- sw.Write(data)
- sw.Close()
- response = CType(request.GetResponse(), HttpWebResponse)
- sr = New StreamReader(response.GetResponseStream())
- responseData = sr.ReadToEnd()
- Return (responseData)
- End Function
- End Class
- Class APIAuth
- Private key As String
- Private secret As String
- Public Sub New(ByVal apikey As String, ByVal apisecret As String)
- key = apikey
- secret = apisecret
- End Sub
- Public Function GenerateAuthorizationHeader(ByVal httpVerb As String, ByVal jsonBodyStr As String, ByVal httpContentType As String, ByVal dateStr As String, ByVal relativeUrl As String) As String
- Dim signature As String = GenerateSigniture(httpVerb, jsonBodyStr, httpContentType, dateStr, relativeUrl)
- Return $"HMAC {key}:{signature}"
- End Function
- Public Function GenerateSigniture(ByVal httpVerb As String, ByVal jsonBodyStr As String, ByVal httpContentType As String, ByVal dateStr As String, ByVal relativeUrl As String) As String
- Dim jsonBodyHash As String = GetMD5Hash(jsonBodyStr)
- Dim signitureData As String = String.Join(vbLf, httpVerb, jsonBodyHash, httpContentType, dateStr, relativeUrl)
- Dim apiSecretBytes As Byte() = Encoding.UTF8.GetBytes(secret)
- Dim signitureBytes As Byte() = Encoding.UTF8.GetBytes(signitureData)
- Dim hmacsha1 = New HMACSHA1(apiSecretBytes, False)
- Dim resultBytes As Byte() = hmacsha1.ComputeHash(signitureBytes)
- Return Convert.ToBase64String(resultBytes)
- End Function
- Private Function GetMD5Hash(ByVal text As String) As String
- Dim returnvalue As StringBuilder = New StringBuilder()
- Dim cryptoSP As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
- Dim HashBytes As Byte() = Encoding.UTF8.GetBytes(text)
- HashBytes = cryptoSP.ComputeHash(HashBytes)
- For Each bte As Byte In HashBytes
- returnvalue.Append(bte.ToString("x2").ToLower())
- Next
- Return returnvalue.ToString()
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement