HUGO
Menu
GitHub 90234 stars Mastodon

crypto.Hash

Returns the checksum of the given input, encoded to a hexadecimal string, using the given hash algorithm.
Syntax
crypto.Hash [ALGORITHM] INPUT
Returns
string

Usage

The crypto.Hash function returns the checksum of the given input, encoded to a hexadecimal string, using the given hash algorithm.

The supported algorithms match those used for the Subresource Integrity (SRI) 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.

Arguments

ALGORITHM
(string) The hash algorithm, one of md5, sha1, sha256, sha384, or sha512. Default is sha256.
INPUT
(string) The value to hash.

Examples

{{ crypto.Hash "sha256" "Hello world" }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c
{{ "Hello world" | crypto.Hash "sha512" }} → b7f783baed8297f0db917462184ff4f08e69c2d5e5f79a942600f9725f58ce1f29c18139bf80b06c0fff2bdd34738452ecf40c488c22a7e3d80cdf6f9c1c0d47

Without an algorithm:

{{ "Hello world" | crypto.Hash }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c

Constructing an SRI hash from a string:

{{ $algo := "sha256" }}
{{ printf "%s-%s" $algo ("Hello world" | crypto.Hash $algo | encoding.HexDecode | base64Encode) }} → sha256-ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw=