diff --git a/go.mod b/go.mod index b53fc2303..1f82c3bf2 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/Masterminds/sprig/v3 v3.2.3 github.com/adrg/xdg v0.4.0 github.com/antonmedv/expr v1.15.3 - github.com/codingsince1985/checksum v1.3.0 github.com/forPelevin/gomoji v1.1.8 github.com/goccy/go-yaml v1.11.2 github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum index 8bccc70f9..d5b0cccdd 100644 --- a/go.sum +++ b/go.sum @@ -62,8 +62,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/codingsince1985/checksum v1.3.0 h1:kqqIqWBwjidGmt/pO4yXCEX+np7HACGx72EB+MkKcVY= -github.com/codingsince1985/checksum v1.3.0/go.mod h1:QfRskdtdWap+gJil8e5obw6I8/cWJ0SwMUACruWDSU8= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -240,7 +238,6 @@ github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0 github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= diff --git a/pkg/checksum/checksum.go b/pkg/checksum/checksum.go index 0a73b2f60..128f4f13d 100644 --- a/pkg/checksum/checksum.go +++ b/pkg/checksum/checksum.go @@ -1,16 +1,17 @@ package checksum import ( + "crypto/md5" //nolint:gosec "crypto/sha1" //nolint:gosec + "crypto/sha256" "crypto/sha512" - "encoding/hex" "errors" "fmt" + "hash" "io" "strings" "github.com/aquaproj/aqua/v2/pkg/config/registry" - "github.com/codingsince1985/checksum" "github.com/spf13/afero" ) @@ -30,38 +31,31 @@ func (*Calculator) Calculate(fs afero.Fs, filename, algorithm string) (string, e } func CalculateReader(file io.Reader, algorithm string) (string, error) { + h, err := getHash(algorithm) + if err != nil { + return "", err + } + if _, err := io.Copy(h, file); err != nil { + return "", fmt.Errorf("copy a io.Reader to hash object: %w", err) + } + return fmt.Sprintf("%x", h.Sum(nil)), nil +} + +func getHash(algorithm string) (hash.Hash, error) { switch algorithm { case "md5": - return checksum.MD5sumReader(file) //nolint:wrapcheck + return md5.New(), nil //nolint:gosec case "sha256": - return checksum.SHA256sumReader(file) //nolint:wrapcheck + return sha256.New(), nil case "sha512": - return calculateSHA512Reader(file) + return sha512.New(), nil case "sha1": - return calculateSHA1Reader(file) + return sha1.New(), nil //nolint:gosec case "": - return "", errors.New("algorithm is required") + return nil, errors.New("algorithm is required") default: - return "", errors.New("unsupported algorithm") - } -} - -func calculateSHA1Reader(file io.Reader) (string, error) { - byt, err := io.ReadAll(file) - if err != nil { - return "", fmt.Errorf("read a file: %w", err) - } - sum := sha1.Sum(byt) //nolint:gosec - return hex.EncodeToString(sum[:]), nil -} - -func calculateSHA512Reader(file io.Reader) (string, error) { - byt, err := io.ReadAll(file) - if err != nil { - return "", fmt.Errorf("read a file: %w", err) + return nil, errors.New("unsupported algorithm") } - sum := sha512.Sum512(byt) - return hex.EncodeToString(sum[:]), nil } func convertChecksumFileName(filename, version string) string {