Skip to main content
sha256(str) returns the lowercase hexadecimal SHA-256 hash of str.

Signature

sha256(str string) string

Example

Compute a cache key based on a lock file’s contents so that the cache is invalidated when dependencies change:
computed "dep_cache_key" {
  description = "Cache key from the Go module lock file"
  expression  = "deps-${sha256(file("go.sum"))}"
}

task "restore-cache" {
  description = "Restore the module cache if available"
  commands    = ["cache restore ${computed.dep_cache_key} || true"]
}

task "install" {
  description = "Download Go module dependencies"
  depends_on  = [task.restore-cache]
  commands    = ["go mod download"]
}

task "save-cache" {
  description = "Save the module cache"
  depends_on  = [task.install]
  commands    = ["cache save ${computed.dep_cache_key}"]
}
The cache key changes whenever go.sum changes, which means the module cache is only reused when the exact same dependencies are declared.

Notes

  • The output is always a 64-character lowercase hexadecimal string.
  • sha256 hashes the string value passed to it. To hash a text file, pass it through file. For binary files, use a shell command like sha256sum instead.
  • For non-security use cases like cache keys, md5 is faster and produces a shorter hash.