Advertisement
Guest User

VB.NET

a guest
Nov 2nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.93 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.IO
  4. Imports System.Net
  5. Imports System.Security.Cryptography
  6. Imports System.Text
  7.  
  8. Class Test
  9.     Public Shared Function Main() As Integer
  10.         Dim auth As APIAuth = New APIAuth("key", "secret")
  11.         Dim client As APIClient = New APIClient("https://business-sandbox.cryptopay.me", auth)
  12.         Dim path As String = "/api/invoices"
  13.         Dim data As String = "{""price_amount"":""0.5"",""price_currency"":""ETH"",""pay_currency"":""ETH""}"
  14.         Dim response As String = client.DoRequest(DateTime.Now, path, data)
  15.         Console.WriteLine(response)
  16.         Return 0
  17.     End Function
  18. End Class
  19.  
  20. Class APIClient
  21.     Private baseURL As String
  22.     Private auth As APIAuth
  23.  
  24.     Public Sub New(ByVal url As String, ByVal apiAuth As APIAuth)
  25.         baseURL = url
  26.         auth = apiAuth
  27.     End Sub
  28.  
  29.     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
  30.         ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
  31.         Dim request As HttpWebRequest = CType(WebRequest.Create(baseURL & path), HttpWebRequest)
  32.         Dim response As HttpWebResponse
  33.         Dim sw As StreamWriter
  34.         Dim sr As StreamReader
  35.         Dim dateStr As String
  36.         Dim responseData As String
  37.         request.Method = verb
  38.         request.ContentType = contentType
  39.         request.ContentLength = data.Length
  40.         request.Date = date
  41.         dateStr = date.ToUniversalTime().ToString("r")
  42.         request.Headers.Add("Authorization", auth.GenerateAuthorizationHeader(verb, data, contentType, dateStr, path))
  43.         sw = New StreamWriter(request.GetRequestStream())
  44.         sw.Write(data)
  45.         sw.Close()
  46.         response = CType(request.GetResponse(), HttpWebResponse)
  47.         sr = New StreamReader(response.GetResponseStream())
  48.         responseData = sr.ReadToEnd()
  49.         Return (responseData)
  50.     End Function
  51. End Class
  52.  
  53. Class APIAuth
  54.     Private key As String
  55.     Private secret As String
  56.  
  57.     Public Sub New(ByVal apikey As String, ByVal apisecret As String)
  58.         key = apikey
  59.         secret = apisecret
  60.     End Sub
  61.  
  62.     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
  63.         Dim signature As String = GenerateSigniture(httpVerb, jsonBodyStr, httpContentType, dateStr, relativeUrl)
  64.         Return $"HMAC {key}:{signature}"
  65.     End Function
  66.  
  67.     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
  68.         Dim jsonBodyHash As String = GetMD5Hash(jsonBodyStr)
  69.         Dim signitureData As String = String.Join(vbLf, httpVerb, jsonBodyHash, httpContentType, dateStr, relativeUrl)
  70.         Dim apiSecretBytes As Byte() = Encoding.UTF8.GetBytes(secret)
  71.         Dim signitureBytes As Byte() = Encoding.UTF8.GetBytes(signitureData)
  72.         Dim hmacsha1 = New HMACSHA1(apiSecretBytes, False)
  73.         Dim resultBytes As Byte() = hmacsha1.ComputeHash(signitureBytes)
  74.         Return Convert.ToBase64String(resultBytes)
  75.     End Function
  76.  
  77.     Private Function GetMD5Hash(ByVal text As String) As String
  78.         Dim returnvalue As StringBuilder = New StringBuilder()
  79.         Dim cryptoSP As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
  80.         Dim HashBytes As Byte() = Encoding.UTF8.GetBytes(text)
  81.         HashBytes = cryptoSP.ComputeHash(HashBytes)
  82.  
  83.         For Each bte As Byte In HashBytes
  84.             returnvalue.Append(bte.ToString("x2").ToLower())
  85.         Next
  86.  
  87.         Return returnvalue.ToString()
  88.     End Function
  89. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement