gohugoio/hugo · error
crypto.Hash: expected 1 or 2 arguments, got
Error message
crypto.Hash: expected 1 or 2 arguments, got %d
What it means
`crypto.Hash` (crypto.go:77-86) accepts either one argument (just the value, defaulting the algorithm to `sha256`) or two arguments (algorithm then value). Any other count — zero args, or three or more — hits the `default` switch arm and returns this error with the actual argument count.
Solutions
- Call with one arg: `{{ crypto.Hash $value }}` (defaults to sha256).
- Call with two args: `{{ crypto.Hash "sha512" $value }}`.
- Remove any extra positional arguments beyond algorithm and value.
Example fix
// before
{{ crypto.Hash }}
// after
{{ crypto.Hash $value }}
{{ crypto.Hash "sha512" $value }} Defensive patterns
Strategy: validation
Validate before calling
{{/* crypto.Hash: 1 arg (value, sha256) or 2 args (algo, value) */}}
{{ crypto.Hash $value }}
{{ crypto.Hash "sha512" $value }} Prevention
- Pass either just the value, or algorithm then value.
- Do not spread extra args or options dicts into crypto.Hash.
- Prefer the single-arg form when sha256 is acceptable.
When it happens
Trigger: `{{ crypto.Hash }}` (no args), `{{ crypto.Hash "sha256" "data" "extra" }}` (3 args), or accidentally spreading an options dict.
Common situations: Forgetting the value; passing algorithm + value + options expecting map-args; template macro forwarding an arg slice.
Related errors
- crypto.Hash: is not a supported hash algorithm
- hmac: is not a supported hash function
- is not a supported encoding method
- wrong number of args for default: want 2 got
- can't evaluate a nil pointer of type
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/731a320832772460.
Report an issue: GitHub.
Appendix: 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 52c9bd7908)