Problems with Freemarker - utils.digest.hmac Hmac SHA256 and base 64 encoding?
Hi @GavinD @ChhamaJ @YuriK DougS
We are trying to use http.client to access a 3rd party system. The API requires us to hash (using HMAC SHA 256) a challenge string with our secret key to gain an access token.
1. <#assign signature = utils.digest.hmac("HmacSHA256", "12345678901234567890", "the quick brown fox jumps over the lazy dog") />
Result = XIDINszyYHIP5vszuWRnyP/4/p/y0COFZHV8tzcxPh4=
Unfortunately we get “Unauthorised” from the remote server. So we tried a simple quick test in PHP
2. echo base64_encode(hash_hmac('sha256', "the quick brown fox jumps over the lazy dog", "12345678901234567890" , true));
Result = XIDINszyYHIP5vszuWRnyP/4/p/y0COFZHV8tzcxPh4=
Ok PHP and Freemarker hash routines agree with the same arguments. Great so what gives? We did some digging and discovered via some sample client code that they base 64 decoded the key. So sticking with PHP we tried
4. echo base64_encode(hash_hmac('sha256', "the quick brown fox jumps over the lazy dog", base64_decode("12345678901234567890") , true));
Result = TWxBl/zreSUOx/NFx+aHNLTU9tPkQ7TLQDPngthWf20=
And this worked !! We now got an access token from the remote service.. So we thought awesome lets do the same in Freemarker
5. <#assign signature = utils.digest.hmac("HmacSHA256", utils.base64.decode("12345678901234567890”), "the quick brown fox jumps over the lazy dog") />
Result = Y+4vRRdPkXF5uRWjtIkEJosBpCHVEb0AqXRPfaNl5Tc=
But we get a different hash? and still no token :(
We suspect something weird is going on with string casting arguments in the Freemarker - because if we try this
utils.base64.encode( utils.base64.decode("12345678901234567890”))
We get this 77+9be+/ve+/ve+/ve+/vU12345677+977+9dA==
With php echo base64_encode( base64_decode("12345678901234567890") )
as expected 12345678901234567890
Any ideas?