crypto.Hash
用给定哈希算法计算给定输入的校验和,并编码为十六进制字符串。
ALGORITHM 取 md5、sha1、sha256(默认)、sha384、sha512 之一:
{{ crypto.Hash "sha256" "Hello world" }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c
{{ "Hello world" | crypto.Hash "sha512" }} → b7f783baed8297f0db917462184ff4f08e69c2d5e5f79a942600f9725f58ce1f29c18139bf80b06c0fff2bdd34738452ecf40c488c22a7e3d80cdf6f9c1c0d47省略算法时默认使用 sha256:
{{ "Hello world" | crypto.Hash }} → 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c支持的算法与带指纹资源的 .Data.Integrity 中子资源完整性哈希所用的算法一致。把 crypto.Hash 与 encoding.HexDecode、encoding.Base64Encode 组合起来,就能从字符串构造出 SRI 哈希:
{{ $algo := "sha256" }}
{{ $integrity := printf "%s-%s" $algo ("Hello world" | crypto.Hash $algo | encoding.HexDecode | encoding.Base64Encode) }}