web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Unable to create the correct HMACSHA256 Hash in AL

(0) ShareShare
ReportReport
Posted on by 5

Hello!

I have the following code i C-sharp below that generates a hashed signature:

HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
        var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

This works fine.

But when I try to replicate this in AL I'm unable to generate the same hash:

Crypto: Codeunit "Cryptography Management";
HashAlgo: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512;

Crypto.GenerateBase64KeyedHashAsBase64String(stringToSign, key, HashAlgo::HMACSHA256);

I would be very grateful if someone could explain to me what I'm doing wrong.

I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,111 Moderator on at

    You have to put the result into a text variable

    MyText:=Crypto.GenerateBase64KeyHasedBase64String......

    It looks to me that you do not fetch the result of the hash process.

  • C_J_S Profile Picture
    5 on at

    I'm sorry for not including the whole code example. But I'm already assigning the rusult to a text variable:

    signature := Crypto.GenerateBase64KeyedHashAsBase64String(stringToSign, keyText, HashAlgo::HMACSHA256);

       procedure GetSignature(Uri: Text; keyName: Text; keyText: Text; ttl: Duration): Text

       var

           Crypto: Codeunit "Cryptography Management";

           TypeHelper: Codeunit "Type Helper";

           expiry: Text;

           stringToSign: Text;

           signature: Text;

           HashAlgo: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512;

       begin

           expiry := GetExpiry(ttl);

           stringToSign := TypeHelper.UrlEncode(Uri) + '\' + 'n' + expiry;

           signature := Crypto.GenerateBase64KeyedHashAsBase64String(stringToSign, keyText, HashAlgo::HMACSHA256);

           exit(signature );

       end;

  • C_J_S Profile Picture
    5 on at

    I was able to solve the problem by following the example in this blogpost:

    futuriot.com/.../

  • PravinDangar Profile Picture
    30 on at

    C_J_S  Can you please share that blog Or sample code How you resolve it ? Thanks in dvance

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans