package transfer

import (
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"io"
	"os"
)

// VerifySha512File checks that a file matches the expected sha512 hash.
func VerifySha512File(path, expected string) error {
	if expected == "" {
		return nil
	}

	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	hasher := sha512.New()
	if _, err := io.Copy(hasher, file); err != nil {
		return err
	}

	actual := hex.EncodeToString(hasher.Sum(nil))
	if actual != expected {
		return fmt.Errorf("sha512 mismatch")
	}

	return nil
}
