I want my sha1 hash in base64. It seems to output a hex string. Is it possible in Hugo to convert a hexadecimal string to base64?
I tried this:
{{ sha1 .Params.password | base64Encode }}
Thank you in advance!
I want my sha1 hash in base64. It seems to output a hex string. Is it possible in Hugo to convert a hexadecimal string to base64?
I tried this:
{{ sha1 .Params.password | base64Encode }}
Thank you in advance!
And what was the result?
The password string (.Params.password) test
resulted in a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
. I need it to output qUqP5cyxm6YcTAhz05Hph5gvu9M=
. In PHP I can convert it using:
echo base64_encode(hex2bin('a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'));
Which outputs:
qUqP5cyxm6YcTAhz05Hph5gvu9M=
I want to do this with Hugo. Any ideas?
Thanks!
Hugo (or Go?) seems to have their own ideas about SHA1. When I run
echo -n "Hello World" |sha1sum
on macOS, I get 0a4d55a8d778e5022fab701977c5d840bbc486d0
.
The same with this online tool.
Hugo’s doc, however, states that
{{ sha1 "Hello world" }} → 7b502c3a1f48c8609ae212cdfb639dee39673f5e
That could mean that either the documentation or the code is wrong. Or something else.
SHA1 of test
is indeed what you say, and the online tool above converts that to the same string as PHP (if set to bin mode).
As the documentation is quite terse, I have no idea what’s going on.
As discussed in another thread, “Hello World” and “Hello world” are different things. Hugo’s crypto functions are doing the right thing, and our documentation is correct.
I’ll add something to the md5
, sha1
, and sha256
descriptions to indicate that the return value is hex (as shown in the examples). Other than that, there’s not a whole lot to explain.
TLDR: I can’t think of way to do this that isn’t… convoluted, and even then I’m not sure it’s possible.
Hugo’s md5
, sha1
, and sha256
crypto functions encode the checksum to hex, and we do not have a “hex2bin” function as shown in your PHP example.
In comparison, Hugo’s hmac
crypto function includes an optional encoding argument, either hex
(default) or binary
. It would be convenient if md5
, sha1
, and sha256
had signatures such as…
sha1 INPUT [ENCODING]
…where ENCODING
is one of hex
(default), binary
, or base64
. And add base64
to the hmac
encodings as well. I’m waffling on the binary
option, though it would provide input for future encoding functions without having to add those options to the crypto functions.
The signature above is backwards compatible.
Or we could look at a hex2bin
function, but I can’t remember a request/need for this functionality outside of your example.
Thank you for your detailed answer Joe! Much appreciated.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.