UTF-8 character encoding when converting strings to bytes

I am trying to see if it is possible to use Hugo’s templates in order to calculate a SHA256 digest by using UTF-8 character encoding when converting strings to bytes, then return the SHA256 digest and encode its bytes with Base64.

So far I have failed and I am not quite sure how to proceed. Also I am not sure whether this is currently supported in Hugo.

Here is an example

The following sample string:

01011193492en0O170911143656Test order some items0.12EURcardlink@cardlink.gr6900000000GRUKvisahttps://euro.test.modirum.com/vpostestsv4/shops/shopdemo.jsp?cmd=confirmhttps://euro.test.modirum.com/vpostestsv4/shops/shopdemo.jsp?cmd=cancelCardlink1```

Needs to produce the following value:

Xw19+XA5lQXbzEHvvFYe1Zrm7N+rvpdIvzuIyM9HY3Q=

The rule for the above is Digest=base64( sha256( utf8bytes(concatenated value) ) )
The recommended way to do the above is server side with Java, ASP etc… and then send the generated digest with a POST request.

But I am interested in seeing whether it is possible to generate a set of these digests in Hugo and then send one according to user selection.

I’ve tried the following along with several syntax variations of the printf function

{{ $digest := base64Encode (sha256 (printf "%U" "01011193492en0O170911143656Test order some items0.12EURcardlink@cardlink.gr6900000000GRUKvisahttps://euro.test.modirum.com/vpostestsv4/shops/shopdemo.jsp?cmd=confirmhttps://euro.test.modirum.com/vpostestsv4/shops/shopdemo.jsp?cmd=cancelCardlink1")) }}

But I did not manage to output the desired value…
Maybe there is a package that needs to be added to Hugo and a new template function that uses UTF-8 encoding when converting strings to bytes.

I am really out of my depth here.

What do you mean by utf8bytes? I can’t find documentation for printf %U. Maybe it’s more a way to make sure, the string is UTF8, not bytes per se. What happens if you use %x instead? That should print out real bytes… the connection of utf8 and bytes is what irritates me somehow.

Golang doc has this to say:

The doc for fmt which is the go package that Hugo uses for the printf function is here

I suppose that it means returning an array of all the bytes in a unicode string (but I may be wrong).

e.g. 48 49 48 49 49 49 57 51 52 57 50 101

I am not getting the desired digest output.

Slight progress. The syntax printf "% x\n" will output the bytes in a unicode string

i.e.

30 31 30 31 31 31 39 33 34 39 32 65 6e 30 4f 31 37 30 39 31 31 31 34 33 36 35 36 54 65 73 74 20 6f 72 64 65 72 20 73 6f 6d 65 20 69 74 65 6d 73 30 2e 31 32 45 55 52 63 61 72 64 6c 69 6e 6b 40 63 61 72 64 6c 69 6e 6b 2e 67 72 36 39 30 30 30 30 30 30 30 30 47 52 55 4b 76 69 73 61 68 74 74 70 73 3a 2f 2f 65 75 72 6f 2e 74 65 73 74 2e 6d 6f 64 69 72 75 6d 2e 63 6f 6d 2f 76 70 6f 73 74 65 73 74 73 76 34 2f 73 68 6f 70 73 2f 73 68 6f 70 64 65 6d 6f 2e 6a 73 70 3f 63 6d 64 3d 63 6f 6e 66 69 72 6d 68 74 74 70 73 3a 2f 2f 65 75 72 6f 2e 74 65 73 74 2e 6d 6f 64 69 72 75 6d 2e 63 6f 6d 2f 76 70 6f 73 74 65 73 74 73 76 34 2f 73 68 6f 70 73 2f 73 68 6f 70 64 65 6d 6f 2e 6a 73 70 3f 63 6d 64 3d 63 61 6e 63 65 6c 43 61 72 64 6c 69 6e 6b 31

I’m still unable to generate the desired digest value with 256SHA and Base64 encoding in Hugo.

Or I’m completely wrong about what UTF-8 character encoding strings to bytes is supposed to be.

Don’t use \n, use a single whitespace. "%x ". That produces a string. You don’t want an array or new lines in there. And try to change between %x and %X, those are uppercase/lowercase variants.

Thanks for the reply.

Turns out that the problem I encountered is unrelated to UTF-8 character encoding.

I will elaborate in another topic later.