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* ?
This commit is contained in:
@@ -8,8 +8,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"io"
|
"io"
|
||||||
|
"encoding/hex"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"compress/flate"
|
"compress/flate"
|
||||||
|
"crypto/sha256"
|
||||||
"github.com/cavaliercoder/go-cpio"
|
"github.com/cavaliercoder/go-cpio"
|
||||||
"github.com/klauspost/pgzip"
|
"github.com/klauspost/pgzip"
|
||||||
"github.com/google/renameio"
|
"github.com/google/renameio"
|
||||||
@@ -52,6 +54,38 @@ func (archive *Archive) Write(path string, mode os.FileMode) error {
|
|||||||
return nil
|
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 {
|
func (archive *Archive) AddFile(file string, dest string) error {
|
||||||
if err := archive.addDir(filepath.Dir(dest)); err != nil {
|
if err := archive.addDir(filepath.Dir(dest)); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user