// Copyright 2018 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
// Package dirhash defines hashes over directory trees.// These hashes are recorded in go.sum files and in the Go checksum database,// to allow verifying that a newly-downloaded module has the expected content.
package dirhashimport ()// DefaultHash is the default hash function used in new go.sum entries.varDefaultHashHash = Hash1// A Hash is a directory hash function.// It accepts a list of files along with a function that opens the content of each file.// It opens, reads, hashes, and closes each file and returns the overall directory hash.typeHashfunc(files []string, open func(string) (io.ReadCloser, error)) (string, error)// Hash1 is the "h1:" directory hash function, using SHA-256.//// Hash1 is "h1:" followed by the base64-encoded SHA-256 hash of a summary// prepared as if by the Unix command://// sha256sum $(find . -type f | sort) | sha256sum//// More precisely, the hashed summary contains a single line for each file in the list,// ordered by [slices.Sort] applied to the file names, where each line consists of// the hexadecimal SHA-256 hash of the file content,// two spaces (U+0020), the file name, and a newline (U+000A).//// File names with newlines (U+000A) are disallowed.func ( []string, func(string) (io.ReadCloser, error)) (string, error) { := sha256.New() = append([]string(nil), ...)slices.Sort()for , := range {ifstrings.Contains(, "\n") {return"", errors.New("dirhash: filenames with newlines are not supported") } , := ()if != nil {return"", } := sha256.New() _, = io.Copy(, ) .Close()if != nil {return"", }fmt.Fprintf(, "%x %s\n", .Sum(nil), ) }return"h1:" + base64.StdEncoding.EncodeToString(.Sum(nil)), nil}// HashDir returns the hash of the local file system directory dir,// replacing the directory name itself with prefix in the file names// used in the hash function.func (, string, Hash) (string, error) { , := DirFiles(, )if != nil {return"", } := func( string) (io.ReadCloser, error) {returnos.Open(filepath.Join(, strings.TrimPrefix(, ))) }return (, )}// DirFiles returns the list of files in the tree rooted at dir,// replacing the directory name dir with prefix in each name.// The resulting names always use forward slashes.func (, string) ([]string, error) {var []string = filepath.Clean() := filepath.Walk(, func( string, os.FileInfo, error) error {if != nil {return }if .IsDir() {returnnil } elseif == {returnfmt.Errorf("%s is not a directory", ) } := if != "." { = [len()+1:] } := filepath.Join(, ) = append(, filepath.ToSlash())returnnil })if != nil {returnnil, }return , nil}// HashZip returns the hash of the file content in the named zip file.// Only the file names and their contents are included in the hash:// the exact zip file format encoding, compression method,// per-file modification times, and other metadata are ignored.func ( string, Hash) (string, error) { , := zip.OpenReader()if != nil {return"", }defer .Close()var []string := make(map[string]*zip.File)for , := range .File { = append(, .Name) [.Name] = } := func( string) (io.ReadCloser, error) { := []if == nil {returnnil, fmt.Errorf("file %q not found in zip", ) // should never happen }return .Open() }return (, )}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)