I’m really grateful for the hmac function but there is something I can’t achieve. It is to output the same string as PHP’s own hmac function with its “raw binary data” mode enabled.
{{ $test := hmac "sha256" "My Secret" "My Message" | base64Encode }}
All good, with the default data mode (hexits lowercase) Hugo and PHP get the same result. Unfortunately, it seems what I need is the output I get with the following where the last parameter is set to true (raw data):
echo base64_encode(hash_hmac('sha256', "My Message", "My Secret", true));
Is there a way to achieve the same result with Hugo?
Yes the argument order is right (different in PHP).
For context, I’m desperately trying to work Apple News API and their security policy requires hmac on various concatenated parameters. I can successfully pass the auth with the php raw data method, but not with Hugo’s own.
{{ hmac "sha256" "My Secret" "My Message" true | encoding.Base64Encode }}
Or, we could add a HexDecode method to the encoding namespace, which wouldn’t mess with the piping sequence:
{{ "My Message" | hmac "sha256" "My Secret" | encoding.HexDecode | encoding.Base64Encode }}
encoding.HexDecode and encoding.HexEncode might also be useful for color conversions. These were implemented (but not merged) by @moorereason in #7605. I suspect these would be used infrequently, so I’m not sure it makes sense to give them aliases (e.g., hexDecode).
Upon further consideration I think it’s better to add an optional arg to the hmac function. We’re dealing with strings not integers, so encode/decode functions are not going to be very useful in other contexts.
But I don’t really like the idea of a boolean arg (e.g., the PHP implementation).
What about this signature instead?
hmac HASH_TYPE KEY MESSAGE [ENCODING]
The ENCODING arg could be “hex” (default, current behavior) or “binary”. The only downside is that you can’t pipe the MESSAGE when specifying ENCODING.
{{ hmac "sha256" "My Secret" "My Message" "binary" | encoding.Base64Encode }}
Thanks, personally I also think the fewest parameters to a function the better but
That would mean create hmac_sha256, hmac_md5 function etc rather than having this hmac catchall function.
As much as I like Go Template Pipes, I don’t like that its usage dictates argument ordering for “non-pipe” usage…
I don’t think a lot of people are using the hmac function as it is, I think even fewer people are using it in a Go Template Pipe context. So we should be safe for breaking code.
I’m not sure what you mean. The proposed implementation is backwards compatible; this isn’t a breaking change. But if you need to specify ENCODING it must be the last arg.
I was thinking this might be only a breaking change for people who have been using this in their code… (very few people I would think):
{{ "My Message" | hmac "sha256" "My Secret" | base64Encode }}
As we now have new potential last argument, but as it’s optional it might not count?
I’m not sure why I added that other comment about pipe and argument ordering, it’s a general feeling I have but nothing in this thread invited this comment…
Thank you so much for implementing this, it’s perfect!