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

  1. Call it as `crypto.Hash VALUE` or `crypto.Hash ALGORITHM VALUE` — exactly one or two arguments.
  2. If you wanted a keyed/encoded digest, use `hmac` instead, which takes hash, key, message, and an optional encoding.
  3. 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

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


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/731a320832772460.json. Report an issue: GitHub ↗.