yao/registry/manager/common/hash.go
Max 44af6ba759 Enhance command structure with MCP and Robot functionalities
- Introduce new MCP and Robot command groups in the CLI, allowing for better organization of package management commands.
- Add corresponding command descriptions for MCP and Robot functionalities to improve user guidance.
- Update the agent command group to include additional commands for enhanced agent management.
- Modify .gitignore to exclude specific design markdown files from version control.
- Add environment variable for test application path in GitHub workflows to streamline testing setup.
2026-03-03 08:11:43 +08:00

60 lines
1.3 KiB
Go

package common
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// HashFile computes the SHA-256 hash of a file and returns it as "sha256-<hex>".
func HashFile(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return fmt.Sprintf("sha256-%x", h.Sum(nil)), nil
}
// HashBytes computes the SHA-256 hash of raw bytes and returns "sha256-<hex>".
func HashBytes(data []byte) string {
h := sha256.Sum256(data)
return fmt.Sprintf("sha256-%x", h[:])
}
// HashDir walks a directory and returns a map of relative paths to their SHA-256 hashes.
// The relPrefix is prepended to each relative path (use "" for no prefix).
func HashDir(dir, relPrefix string) (map[string]string, error) {
result := map[string]string{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
rel, err := filepath.Rel(dir, path)
if err != nil {
return err
}
rel = filepath.ToSlash(rel)
if relPrefix != "" {
rel = strings.TrimRight(filepath.ToSlash(relPrefix), "/") + "/" + rel
}
hash, err := HashFile(path)
if err != nil {
return err
}
result[rel] = hash
return nil
})
return result, err
}