Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / How to use HMACSHA256 ...
Finance forum
Answered

How to use HMACSHA256 in F&O

Posted on by 3,041
Hi,
 
I have a requirement where I need to generate the authentication signature using .Net class HMACSHA256. Does any one know how we can use this in x++. I have key and data using these values we have to generate hexa signature.
Categories:
  • Verified answer
    Pooja Karki Profile Picture
    Pooja Karki 3,041 on at
    How to use HMACSHA256 in F&O
    Thanks for suggestion , I am able to generate signature using below code.
     
    str key, data;
    System.Text.StringBuilder sBuilder;
    System.Array resultByteArray;
    int arrayLength ;
    ClrObject clrStrObject;
    ClrObject clrStr;
    int i;
    str signature;
    key='67be6c44----a34fa4f1abb6';
    data='xxxxxxxxxxxxxxxxxxxx';
    System.Security.Cryptography.HMACSHA256 hmac=new System.Security.Cryptography.HMACSHA256();
    hmac.Key=System.Text.Encoding::UTF8.GetBytes(key);
    resultByteArray=hmac.ComputeHash(System.Text.Encoding::UTF8.GetBytes(data));
    sBuilder = new System.Text.StringBuilder();
    arrayLength = resultByteArray.get_Length() ;
    for (i = 0; i <arrayLength; i++)
    {
        clrStrObject = resultByteArray.GetValue(i);
        clrStr = clrStrObject.ToString('x2');
        sBuilder.Append(clrStr);
    }
    signature = sBuilder.ToString();
    info(strFmt("%1",signature));
    }
  • Martin Dráb Profile Picture
    Martin Dráb 225,672 Super User on at
    How to use HMACSHA256 in F&O
    You seem to implicitly convert a byte array to string, which calls ToString() method of the array and it gives you this result.
     
    First of all, ask yourself whether you need to convert the hash to a string at all. If so, think about how you want to convert it. Then write code doing that.
  • Pooja Karki Profile Picture
    Pooja Karki 3,041 on at
    How to use HMACSHA256 in F&O
    Hi,
     
    Thanks for response I have refered both link and written below code and getting below output in X++.  I am not sure what wrong I was doing.
     
    Code output: "System.Byte[]"
     
    str key, data;
    System.Text.StringBuilder sBuilder;
    ClrObject obj;
    str signature;
    key='67be6c44----a34fa4f1abb6';
    data='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    System.Security.Cryptography.HMACSHA256 hmac=new System.Security.Cryptography.HMACSHA256();
    hmac.Key=System.Text.Encoding::UTF8.GetBytes(key);
    hmac.ComputeHash(System.Text.Encoding::UTF8.GetBytes(data));
    obj=hmac.Hash;
    sBuilder = new System.Text.StringBuilder();
    sBuilder.Append(obj);
    signature=sBuilder.ToString();
    info(strFmt("%1",signature));
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 225,672 Super User on at
    How to use HMACSHA256 in F&O
    You can use System.Security.Cryptography.HMACSHA256 class via .NET Interop. It's included in the .NET Framework.
  • Hana Xue Profile Picture
    Hana Xue Microsoft Employee on at
    How to use HMACSHA256 in F&O
    Hi,
    Before the developer offers professional advice, hopefully the following link will be helpful.
    GitHub - hadmacker/HmacSignature: HMAC SHA256 Signature implementation using .NET Core and xUnit
    Best Regards,
    Hana

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,420 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,672 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans