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.

  • PravinDangar Profile Picture
    30 on at
    RE: Unable to create the correct HMACSHA256 Hash in AL

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

  • C_J_S Profile Picture
    5 on at
    RE: Unable to create the correct HMACSHA256 Hash in AL

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

    futuriot.com/.../

  • C_J_S Profile Picture
    5 on at
    RE: Unable to create the correct HMACSHA256 Hash in AL

    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;

  • Suggested answer
    Inge M. Bruvik Profile Picture
    999 Moderator on at
    RE: Unable to create the correct HMACSHA256 Hash in AL

    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.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,278 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,011 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans