(NET) NET (2016)

Standard HMACSHA256 Digital signature in VB.NET

I have wrote a lot of various Payment gateways (for example this is one of the my last Gateway to PayMaster) and below I want to see my code of HMACSHA256 Sign. In this case I want to sign JSON before it going with the request to the server.


   1:      Public Function Create_HMACSHA256_Sign(ByVal SecretKey As String, ByVal Message As String) As String
   2:          Dim Encoding = New Text.ASCIIEncoding()
   3:          Dim KeyByte As Byte() = Encoding.GetBytes(SecretKey)
   4:          Dim MessageBytes As Byte() = Encoding.GetBytes(Message)
   5:          Using Hmacsha256 = New HMACSHA256(KeyByte)
   6:              Dim HashBytes As Byte() = Hmacsha256.ComputeHash(MessageBytes)
   7:              Return BitConverter.ToString(HashBytes).Replace("-", "").ToLower()
   8:          End Using
   9:      End Function

For using this module need to receive SecretKey from server firstly. Then need to calculate Digital Sign and in one case sign need to add to request header, but in this case I insert digital sign inside request body.


 435:          Dim NewOrderRequest As New RequestOrder With {
 436:          .symbol = SelectedSymbolTextBox1.Text,
 437:          .type = "LIMIT",
 438:          .timeInForce = "GTC",
 439:          .side = IIf(DealTypeRadioButtonSell1.Checked, "SELL", "BUY"),
 440:          .price = NewOrderPriceNumericUpDown1.Value,
 441:          .quantity = NewOrderAmountNumericUpDown1.Value,
 442:          .signature = ""
 443:          }
 444:          Dim JsonString As String = JsonConvert.SerializeObject(NewOrderRequest, WebApi2.DoubleNumericFormaterSerializerSettings)
 445:          Dim DigitalSignature As String = Create_HMACSHA256_Sign(FullAccessSecretKey, JsonString)
 446:          NewOrderRequest.signature = DigitalSignature
 447:          Dim NewOrderRes = Folex.PutWithAPIKeyHeader("/api/automated/order", FullAccessKey, NewOrderRequest)
 448:          If NewOrderRes.IsSuccess Then

This is view of request (below) and response (above) of this server.




Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19
Link to this page: http://www.vb-net.com/HMACSHA256/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <MAIL ME>  <ABOUT ME>  < THANKS ME>