package openwrt

import (
	"crypto/sha256"
	"encoding/hex"
	"io"
	"os"

	"github.com/pkg/errors"
)

func hash(path string) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", errors.WithStack(err)
	}

	hasher := sha256.New()

	defer func() {
		if err := file.Close(); err != nil {
			panic(errors.WithStack(err))
		}
	}()

	if _, err := io.Copy(hasher, file); err != nil {
		return "", errors.WithStack(err)
	}

	return hex.EncodeToString(hasher.Sum(nil)), nil
}