Introduces core functionality for the Agate library, including snapshot creation, restoration, listing, and deletion. Adds examples for basic usage, gRPC proto definitions, and build/configuration files such as `go.mod` and `Makefile`. The implementation establishes the framework for store integration and placeholder server functionality.
33 lines
1.1 KiB
Go
33 lines
1.1 KiB
Go
package hash
|
|
|
|
import (
|
|
"crypto/sha256" // Используем SHA256
|
|
"encoding/hex" // Для преобразования хеша в строку
|
|
"fmt" // Для форматирования ошибок
|
|
"io" // Для копирования данных файла в хешер
|
|
"os" // Для открытия файла
|
|
)
|
|
|
|
// CalculateFileHash вычисляет SHA-256 хеш файла по указанному пути.
|
|
// Возвращает хеш в виде шестнадцатеричной строки и ошибку, если возникли проблемы
|
|
// при чтении файла или вычислении хеша.
|
|
func CalculateFileHash(filePath string) (string, error) {
|
|
file, err := os.Open(filePath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to open file %s: %w", filePath, err)
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
hasher := sha256.New()
|
|
|
|
if _, err = io.Copy(hasher, file); err != nil {
|
|
return "", fmt.Errorf("failed to read file %s for hashing: %w", filePath, err)
|
|
}
|
|
|
|
hashBytes := hasher.Sum(nil)
|
|
hashString := hex.EncodeToString(hashBytes)
|
|
|
|
return hashString, nil
|
|
}
|