package engine import ( "crypto/sha256" "encoding/hex" "fmt" "io/ioutil" "time" "github.com/deranjer/gvc/common/database" ) // UniqueFileHash uses SHA256 to create a hash of the file func UniqueFileHash(src string) ([]byte, error) { file, err := ioutil.ReadFile(src) if err != nil { return []byte{}, err } hasher := sha256.New() hasher.Write(file) hash := hasher.Sum(nil) return hash, nil } // CreateCommitHash creates a hash of all the files and time and commit message func CreateCommitHash(fileList []database.File, commitMessage string) (hash []byte, err error) { hasher := sha256.New() for _, file := range fileList { var err error err = file.CalculateHash() if err != nil { return nil, fmt.Errorf("unable to calculate hash for file: %s with error: %s", file.Path, err) } hasher.Write(file.Hash[:]) } time := time.Now() // Adding the metadata to the hash hasher.Write([]byte(commitMessage + time.String())) hashBytes := hasher.Sum(nil) // Getting the hash bytes fullHash := hex.EncodeToString(hashBytes) fmt.Println("Commit hash: ", fullHash) return hashBytes, nil }