crypto.Hash
Hashes the given input with the given algorithm and returns its checksum encoded to a hexadecimal string.
Syntax
crypto.Hash [ALGORITHM] INPUT
Returns
string
The ALGORITHM is one of md5, sha1, sha256 (the default), sha384, or sha512:
{{ crypto.Hash "sha256" "Hello world" }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c
{{ "Hello world" | crypto.Hash "sha512" }} → b7f783baed8297f0db917462184ff4f08e69c2d5e5f79a942600f9725f58ce1f29c18139bf80b06c0fff2bdd34738452ecf40c488c22a7e3d80cdf6f9c1c0d47If you omit the algorithm, it defaults to sha256:
{{ "Hello world" | crypto.Hash }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3cThe supported algorithms match those used for the Subresource Integrity hash in .Data.Integrity on a fingerprinted resource. Combine crypto.Hash with encoding.HexDecode and encoding.Base64Encode to construct an SRI hash from a string:
{{ $algo := "sha256" }}
{{ $integrity := printf "%s-%s" $algo ("Hello world" | crypto.Hash $algo | encoding.HexDecode | encoding.Base64Encode) }}