Skip to content

Commit

Permalink
hash: do not salt with Go runtime version (#292)
Browse files Browse the repository at this point in the history
Currently the cache code, taken directly from the Go internal
code, salts every hash with the current Go runtime version.
This is appropriate for Go itself but not for here where
the cache package is intended for more general use.

People that need the hashes to depend on the Go runtime version
can explicitly include it as part of the hash.
  • Loading branch information
rogpeppe authored Mar 6, 2025
1 parent 9ee3698 commit fa847a1
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 24 deletions.
18 changes: 0 additions & 18 deletions cache/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"hash"
"io"
"os"
"runtime"
"strings"
"sync"
)
Expand All @@ -29,22 +28,6 @@ type Hash struct {
buf *bytes.Buffer // for verify
}

// hashSalt is a salt string added to the beginning of every hash
// created by NewHash. Using the Go version makes sure that different
// versions of the go command (or even different Git commits during
// work on the development branch) do not address the same cache
// entries, so that a bug in one version does not affect the execution
// of other versions. This salt will result in additional ActionID files
// in the cache, but not additional copies of the large output files,
// which are still addressed by unsalted SHA256.
//
// We strip any GOEXPERIMENTs the go tool was built with from this
// version string on the assumption that they shouldn't affect go tool
// execution. This allows bootstrapping to converge faster: dist builds
// go_bootstrap without any experiments, so by stripping experiments
// go_bootstrap and the final go binary will use the same salt.
var hashSalt = []byte(stripExperiment(runtime.Version()))

// stripExperiment strips any GOEXPERIMENT configuration from the Go
// version string.
func stripExperiment(version string) string {
Expand Down Expand Up @@ -81,7 +64,6 @@ func NewHash(name string) *Hash {
if debugHash {
fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name)
}
h.Write(hashSalt)
if verify {
h.buf = new(bytes.Buffer)
}
Expand Down
6 changes: 0 additions & 6 deletions cache/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
)

func TestHash(t *testing.T) {
oldSalt := hashSalt
hashSalt = nil
defer func() {
hashSalt = oldSalt
}()

h := NewHash("alice")
h.Write([]byte("hello world"))
sum := fmt.Sprintf("%x", h.Sum())
Expand Down

0 comments on commit fa847a1

Please sign in to comment.