2025-01-01 22:18:08 +01:00
|
|
|
package core
|
|
|
|
|
2025-04-13 12:50:35 +02:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
2025-01-01 22:18:08 +01:00
|
|
|
|
2025-04-13 12:50:35 +02: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 {
|
2025-04-13 12:50:35 +02:00
|
|
|
return file1.Metadata.Hash == file2.Metadata.Hash
|
2025-01-01 22:18:08 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 12:50:35 +02: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
|
|
|
|
}
|