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

Announcements

No record found.

News and Announcements icon
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,161 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,091 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,032 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 946 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans