Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
I have a problem to format string to sign webrequest using SHA-256.
I have an example code written on Pyton which works correct.
I try to write the same on .Net but have no success with it.
Thanks for any help!

What I have tried:

Imports System.Security.Cryptography
Imports System.Text

Module Module1

    Sub Main()
        Dim secret_key = "a7d0cb1da1ddbc86c96ee5fedd341b7d8ebfbb2f5c83cfe0909f4e57f05dd403"
        Dim plain_text = String.Join(vbCrLf, New String() {"POST", "https://sellerapi.kaufland.com/v2/units/", "", "1411055926"})
        Dim lcSignature = getShopSignature(secret_key, plain_text)
        Console.WriteLine(lcSignature)
        Console.ReadLine()
        'result must be  "da0b65f51c0716c1d3fa658b7eaf710583630a762a98c9af8e9b392bd9df2e2a"
    End Sub

    Private Function getShopSignature(tcKey As String, tcMessage As String) As String
        Dim lcKeyBytes As Byte() = Encoding.Default.GetBytes(tcKey)
        Dim lcMessageBytes As Byte() = Encoding.Default.GetBytes(tcMessage)
        Using loHmacSha256 As New HMACSHA256(lcKeyBytes)
            Dim lcHashMessage As Byte() = loHmacSha256.ComputeHash(lcMessageBytes)
            Dim lcHexString As String = byteArrayToString(lcHashMessage)
            Return lcHexString
        End Using
    End Function

    Public Function byteArrayToString(ByVal ba As Byte()) As String
        Dim hex As StringBuilder = New StringBuilder(ba.Length * 2)
        For Each b In ba
            hex.AppendFormat("{0:x2}", b)
        Next
        Return hex.ToString()
    End Function

End Module


import hmac
import hashlib
import time

def sign_request(method, uri, body, timestamp, secret_key):
    


    digest_maker = hmac.new(secret_key.encode(), None, hashlib.sha256)
    digest_maker.update(plain_text.encode())
    test = digest_maker.digest()
    return digest_maker.hexdigest()

method = "POST"
uri = "https://sellerapi.kaufland.com/v2/units/"
body = ""
timestamp = 1411055926 # time.time()
secret_key = "a7d0cb1da1ddbc86c96ee5fedd341b7d8ebfbb2f5c83cfe0909f4e57f05dd403"

print(sign_request(method, uri, body, timestamp, secret_key))
# da0b65f51c0716c1d3fa658b7eaf710583630a762a98c9af8e9b392bd9df2e2a
Posted
Updated 8-Aug-23 5:15am
Comments
Andre Oosthuizen 8-Aug-23 9:21am    
Quote:but have no success with it

We will need much more than this. What happens when you run the code, does it error and where...
DrAibolit 8-Aug-23 11:16am    
Hi Andre, thanks for attention to this problem. My mistake was using vbCrlf instead vbLf to separate body items. In case when I change it to vbLf - both code return identical result
Andre Oosthuizen 8-Aug-23 13:34pm    
You're welcome, glad you found the solution.

1 solution

I found my mistake. I use vcCrlf to split plainText
String.Join(vbCrLf, New String()

But I need to use just vbLf instead.
This mistake made my signed request corrupted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900