Search code examples
base64power-automatesha256xxd

Base-64 Encoding of SHA256 in Power Automate


We are trying to implement a hashing algorithm that takes the string 35FEBEA52A07816B0949505E46E6C8CB2B9D3896A625286BFA4CB04202452157 and produces the output Nf6+pSoHgWsJSVBeRubIyyudOJamJShr+kywQgJFIVc=. The documentation states this is the Base-64 encoding of the string, however, using the base64 expression in Power Automate with the above input string yields the output MzVGRUJFQTUyQTA3ODE2QjA5NDk1MDVFNDZFNkM4Q0IyQjlEMzg5NkE2MjUyODZCRkE0Q0IwNDIwMjQ1MjE1Nw==.

My colleague was able to achieve the conversion through command-line with the following command: echo "35FEBEA52A07816B0949505E46E6C8CB2B9D3896A625286BFA4CB04202452157" | xxd -r -p | base64 - however, I can't find any equivalent of this in Power Automate. Any ideas are appreciated. Thanks!


Solution

  • Yep, I have an answer for you but you may not like it. The easiest way is to use a third party connector called Advanced Data Operations and in particular, the C# Script Execute operation.

    https://statesolutions.com.au/c-script-execute/

    https://statesolutions.com.au/pricing/

    You can sign up for a free trial and give it a whirl by going to the Pricing page.

    Flow

    Script

    var hexString = parameters.hexString;
    
    byte[] byteArray = new byte[hexString.Length / 2];
    
    for (int i = 0; i < byteArray.Length; i++)
    {
        byteArray[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    }
    
    var base64String = Convert.ToBase64String(byteArray);
    
    return base64String;
    

    Parameters

    {
      "hexString": @{variables('Hex String')}
    }
    

    Result

    Result