gohugoio/hugo · error
crypto.Hash: expected 1 or 2 arguments, got %d
Error message
crypto.Hash: expected 1 or 2 arguments, got %d
What it means
`crypto.Hash` accepts either one argument (the value, hashed with the default sha256) or two arguments (algorithm name, then value). Any other arity — zero arguments or three or more — hits the switch default and returns this error before any hashing happens.
Source
Thrown at tpl/crypto/crypto.go:85
hash := sha256.Sum256([]byte(conv))
return hex.EncodeToString(hash[:]), nil
}
// Hash returns the hex-encoded checksum of v using the given algorithm; one of
// md5, sha1, sha256 (the default), sha384 or sha512.
//
// The supported algorithms match those used for the Subresource Integrity (SRI)
// hash in .Data.Integrity on fingerprinted resources, so an SRI hash can be
// constructed by combining this with encoding.HexDecode and encoding.Base64Encode.
func (ns *Namespace) Hash(args ...any) (string, error) {
var algo, v any
switch len(args) {
case 1:
algo, v = "sha256", args[0]
case 2:
algo, v = args[0], args[1]
default:
return "", fmt.Errorf("crypto.Hash: expected 1 or 2 arguments, got %d", len(args))
}
conv, err := cast.ToStringE(v)
if err != nil {
return "", err
}
algoS, err := cast.ToStringE(algo)
if err != nil {
return "", err
}
h, err := newHash(algoS)
if err != nil {
return "", err
}
if _, err := h.Write([]byte(conv)); err != nil {View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Call it as `crypto.Hash VALUE` or `crypto.Hash ALGORITHM VALUE` — exactly one or two arguments.
- If you wanted a keyed/encoded digest, use `hmac` instead, which takes hash, key, message, and an optional encoding.
- Quote multi-word string arguments so they aren't parsed as separate arguments.
Example fix
<!-- before -->
{{ crypto.Hash "sha256" $content "hex" }}
<!-- after -->
{{ crypto.Hash "sha256" $content }} Defensive patterns
Strategy: validation
Validate before calling
{{/* call with exactly (options?, data): */}}
{{ crypto.Hash "sha256" "data" }} or {{ "data" | crypto.Hash "sha256" }} Prevention
- crypto.Hash takes 1 or 2 arguments — algorithm plus data; never pipe extra values
- Check pipeline order: `| crypto.Hash "sha256"` appends data as the last arg
- Consult the Hugo docs signature before adding option maps
When it happens
Trigger: `{{ crypto.Hash }}` with no arguments; `{{ crypto.Hash "sha256" $v $extra }}` with three arguments; passing an encoding argument by analogy with `hmac`, which does take an optional encoding — `crypto.Hash` does not.
Common situations: Copying an `hmac` call pattern (which takes hash, key, message, encoding) to `crypto.Hash`; splitting a value into multiple template arguments accidentally (unquoted strings with spaces); pipelines that append an extra final argument, e.g. `{{ $v | crypto.Hash "sha256" "hex" }}`.
Related errors
- need at least 2 arguments to append
- must provide a Resource and optionally an options map
- operator argument must be string type
- can't evaluate the array by no match argument or more than o
- invalid arguments supplied to `time`
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/731a320832772460.json.
Report an issue: GitHub ↗.