From 766da6a0dcd5976d668df39d5f5947092ece429f Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Tue, 10 Aug 2021 13:18:18 -0700 Subject: [PATCH] pkg/archive: add function for checksumming a file This uses sha256 which, after benchmarking, doesn't seem to be any faster or slower than sha1. md5 was surprisingly slower (on aarch64), maybe because there are some CPU accelerated things in sha* ? --- pkgs/archive/archive.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkgs/archive/archive.go b/pkgs/archive/archive.go index 938a9dd..2fb7b69 100644 --- a/pkgs/archive/archive.go +++ b/pkgs/archive/archive.go @@ -8,8 +8,10 @@ import ( "os" "strings" "io" + "encoding/hex" "path/filepath" "compress/flate" + "crypto/sha256" "github.com/cavaliercoder/go-cpio" "github.com/klauspost/pgzip" "github.com/google/renameio" @@ -52,6 +54,38 @@ func (archive *Archive) Write(path string, mode os.FileMode) error { return nil } +func checksum(path string) (string, error) { + var sum string + + buf := make([]byte, 64*1024) + sha256 := sha256.New() + fd, err := os.Open(path) + defer fd.Close() + + if err != nil { + log.Print("Unable to checksum: ", path) + return sum, err + } + + // Read file in chunks + for { + bytes, err := fd.Read(buf) + if bytes > 0 { + _, err := sha256.Write(buf[:bytes]) + if err != nil { + log.Print("Unable to checksum: ", path) + return sum, err + } + } + + if err == io.EOF { + break + } + } + sum = hex.EncodeToString(sha256.Sum(nil)) + return sum, nil +} + func (archive *Archive) AddFile(file string, dest string) error { if err := archive.addDir(filepath.Dir(dest)); err != nil { return err