Debian Codegraph @

docs/content/en/functions/crypto/Hash.md

raw
1 ---
2 title: crypto.Hash
3 description: Returns the checksum of the given input, encoded to a hexadecimal string, using the given hash algorithm.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: string
10 signatures: ['crypto.Hash [ALGORITHM] INPUT']
11 ---
12
13 The `ALGORITHM` is one of `md5`, `sha1`, `sha256` (the default), `sha384`, or `sha512`:
14
15 ```go-html-template
16 {{ crypto.Hash "sha256" "Hello world" }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c
17 {{ "Hello world" | crypto.Hash "sha512" }} → b7f783baed8297f0db917462184ff4f08e69c2d5e5f79a942600f9725f58ce1f29c18139bf80b06c0fff2bdd34738452ecf40c488c22a7e3d80cdf6f9c1c0d47
18 ```
19
20 If you omit the algorithm, it defaults to `sha256`:
21
22 ```go-html-template
23 {{ "Hello world" | crypto.Hash }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c
24 ```
25
26 The 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:
27
28 ```go-html-template
29 {{ $algo := "sha256" }}
30 {{ $integrity := printf "%s-%s" $algo ("Hello world" | crypto.Hash $algo | encoding.HexDecode | encoding.Base64Encode) }}
31 ```
32
33 [Subresource Integrity]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
34 [`.Data.Integrity`]: /methods/resource/data/
35 [`encoding.Base64Encode`]: /functions/encoding/base64encode/
36 [`encoding.HexDecode`]: /functions/encoding/hexdecode/