1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 var debugHash = false // set when GODEBUG=gocachehash=1
19 // HashSize is the number of bytes in a hash.
22 // A Hash provides access to the canonical hash function used to index the cache.
23 // The current implementation uses salted SHA256, but clients must not assume this.
26 name string // for debugging
27 buf *bytes.Buffer // for verify
30 // hashSalt is a salt string added to the beginning of every hash
31 // created by NewHash. Using the Staticcheck version makes sure that different
32 // versions of the command do not address the same cache
33 // entries, so that a bug in one version does not affect the execution
34 // of other versions. This salt will result in additional ActionID files
35 // in the cache, but not additional copies of the large output files,
36 // which are still addressed by unsalted SHA256.
39 func SetSalt(b []byte) {
43 // Subkey returns an action ID corresponding to mixing a parent
44 // action ID with a string description of the subkey.
45 func Subkey(parent ActionID, desc string) ActionID {
47 h.Write([]byte("subkey:"))
53 fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out)
57 hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc)
63 // NewHash returns a new Hash.
64 // The caller is expected to Write data to it and then call Sum.
65 func NewHash(name string) *Hash {
66 h := &Hash{h: sha256.New(), name: name}
68 fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name)
72 h.buf = new(bytes.Buffer)
77 // Write writes data to the running hash.
78 func (h *Hash) Write(b []byte) (int, error) {
80 fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b)
88 // Sum returns the hash of the data written previously.
89 func (h *Hash) Sum() [HashSize]byte {
90 var out [HashSize]byte
93 fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out)
97 if hashDebug.m == nil {
98 hashDebug.m = make(map[[HashSize]byte]string)
100 hashDebug.m[out] = h.buf.String()
106 // In GODEBUG=gocacheverify=1 mode,
107 // hashDebug holds the input to every computed hash ID,
108 // so that we can work backward from the ID involved in a
109 // cache entry mismatch to a description of what should be there.
110 var hashDebug struct {
112 m map[[HashSize]byte]string
115 // reverseHash returns the input used to compute the hash id.
116 func reverseHash(id [HashSize]byte) string {
123 var hashFileCache struct {
125 m map[string][HashSize]byte
128 // FileHash returns the hash of the named file.
129 // It caches repeated lookups for a given file,
130 // and the cache entry for a file can be initialized
131 // using SetFileHash.
132 // The hash used by FileHash is not the same as
133 // the hash used by NewHash.
134 func FileHash(file string) ([HashSize]byte, error) {
136 out, ok := hashFileCache.m[file]
137 hashFileCache.Unlock()
144 f, err := os.Open(file)
147 fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
149 return [HashSize]byte{}, err
151 _, err = io.Copy(h, f)
155 fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
157 return [HashSize]byte{}, err
161 fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out)
164 SetFileHash(file, out)
168 // SetFileHash sets the hash returned by FileHash for file.
169 func SetFileHash(file string, sum [HashSize]byte) {
171 if hashFileCache.m == nil {
172 hashFileCache.m = make(map[string][HashSize]byte)
174 hashFileCache.m[file] = sum
175 hashFileCache.Unlock()