Add initial implementation of Agate snapshot library
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.
This commit is contained in:
32
hash/hash.go
Normal file
32
hash/hash.go
Normal file
@ -0,0 +1,32 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user