github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/cache/hash.go (about) 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. 4 5 package cache 6 7 import ( 8 "github.com/shogo82148/std/bytes" 9 "github.com/shogo82148/std/hash" 10 ) 11 12 // HashSize is the number of bytes in a hash. 13 const HashSize = 32 14 15 // A Hash provides access to the canonical hash function used to index the cache. 16 // The current implementation uses salted SHA256, but clients must not assume this. 17 type Hash struct { 18 h hash.Hash 19 name string 20 buf *bytes.Buffer 21 } 22 23 // Subkey returns an action ID corresponding to mixing a parent 24 // action ID with a string description of the subkey. 25 func Subkey(parent ActionID, desc string) ActionID 26 27 // NewHash returns a new Hash. 28 // The caller is expected to Write data to it and then call Sum. 29 func NewHash(name string) *Hash 30 31 // Write writes data to the running hash. 32 func (h *Hash) Write(b []byte) (int, error) 33 34 // Sum returns the hash of the data written previously. 35 func (h *Hash) Sum() [HashSize]byte 36 37 // FileHash returns the hash of the named file. 38 // It caches repeated lookups for a given file, 39 // and the cache entry for a file can be initialized 40 // using SetFileHash. 41 // The hash used by FileHash is not the same as 42 // the hash used by NewHash. 43 func FileHash(file string) ([HashSize]byte, error) 44 45 // SetFileHash sets the hash returned by FileHash for file. 46 func SetFileHash(file string, sum [HashSize]byte)