CoSync/internal/core/utils.go

37 lines
671 B
Go
Raw Permalink Normal View History

2025-01-01 22:18:08 +01:00
package core
import (
"crypto/sha256"
"io"
"os"
)
2025-01-01 22:18:08 +01:00
// Panic if e contains an error
func Check(e error) {
if e != nil {
panic("Check: A fatal error occured during execution -> " + e.Error())
}
}
// Returns true if files correspond
2025-01-01 22:18:08 +01:00
func CompareFiles(file1 File, file2 File) bool {
return file1.Metadata.Hash == file2.Metadata.Hash
2025-01-01 22:18:08 +01:00
}
// Gets the sha256 hash of a file's content
func GetHash(file File) ([]byte, error) {
// init
hasher := sha256.New()
file_path := "" //TODO: missing
// Open file
f, err := os.Open(file_path)
if err != nil { // Path error
return nil, err
}
// Compute hash
io.Copy(hasher, f)
sum := hasher.Sum(nil)
return sum, nil
}