Azure API Management | SHA 256

Abhay Chandramouli 1,056 Reputation points
2023-06-14T08:22:01.0266667+00:00

Hi

I want to hash in SHA256 in Azure APIM

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,454 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sonny Gillissen 3,751 Reputation points Volunteer Moderator
    2023-06-15T09:02:33.1633333+00:00

    Hi Abhay Chandramouli

    Thanks for reaching out on Microsoft Q&A!

    The following code snippet allows you to SHA256 hash a string in the Azure API Management policy, where you can replace <signing-key> with your own signing key, and <string-to-sign> with the value you want to hash:

      @{  
           System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(<signing-key>);  
           return Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(<string-to-sign>)));  
       }  
    

    Please click 'Accept answer' if you find this helpful.
    Feel free to drop additional queries in the comments below!

    Kind regards,

    Sonny

    1 person found this answer helpful.
    0 comments No comments

  2. Abhishek Sinha 66 Reputation points
    2024-05-27T16:36:06.7133333+00:00

    Hi @Sonny Gillissen Is there any way to set the calculated Hash in an APIM variable using set-variable policy, I am trying to compare the hash sent by front end and calculated by APIM.

    or is there any way to create the hash and compare the same with the one sent in header of the request.

    Whenever I a m trying to set value using set-variable APIM policy I am getting error "cannot convert from 'string' to 'byte[]'". I tried methods given here to convert byte array to string but Its always giving the same error.

    Best regards,

    Abhishek

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.