2024-01-04 22:26:33 +00:00
|
|
|
package plugin
|
2016-08-26 08:38:22 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-04 22:26:33 +00:00
|
|
|
"crypto/md5" //nolint:gosec
|
|
|
|
"crypto/sha1" //nolint:gosec
|
2016-08-26 08:38:22 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha512"
|
2024-01-04 22:26:33 +00:00
|
|
|
"errors"
|
2016-08-26 08:38:22 +00:00
|
|
|
"fmt"
|
|
|
|
"hash/adler32"
|
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
"os"
|
2024-05-07 10:09:08 +00:00
|
|
|
"path/filepath"
|
2022-12-07 13:31:11 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/blake2b"
|
|
|
|
"golang.org/x/crypto/blake2s"
|
2016-08-26 08:38:22 +00:00
|
|
|
)
|
|
|
|
|
2024-01-04 22:26:33 +00:00
|
|
|
var ErrHashMethodNotSupported = errors.New("hash method not supported")
|
|
|
|
|
2024-05-07 10:09:08 +00:00
|
|
|
// Checksum calculates the checksum of the given io.Reader using the specified hash method.
|
|
|
|
// Supported hash methods are: "md5", "sha1", "sha256", "sha512", "adler32", "crc32", "blake2b", "blake2s".
|
|
|
|
func Checksum(r io.Reader, method string) (string, error) {
|
2022-12-07 13:31:11 +00:00
|
|
|
b, err := io.ReadAll(r)
|
2016-08-26 08:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
case "md5":
|
2024-01-04 22:26:33 +00:00
|
|
|
//nolint:gosec
|
2016-08-26 08:38:22 +00:00
|
|
|
return fmt.Sprintf("%x", md5.Sum(b)), nil
|
|
|
|
case "sha1":
|
2024-01-04 22:26:33 +00:00
|
|
|
//nolint:gosec
|
2016-08-26 08:38:22 +00:00
|
|
|
return fmt.Sprintf("%x", sha1.Sum(b)), nil
|
|
|
|
case "sha256":
|
|
|
|
return fmt.Sprintf("%x", sha256.Sum256(b)), nil
|
|
|
|
case "sha512":
|
|
|
|
return fmt.Sprintf("%x", sha512.Sum512(b)), nil
|
|
|
|
case "adler32":
|
2024-05-07 10:09:08 +00:00
|
|
|
return fmt.Sprintf("%08x", adler32.Checksum(b)), nil
|
2016-08-26 08:38:22 +00:00
|
|
|
case "crc32":
|
2024-05-07 10:09:08 +00:00
|
|
|
return fmt.Sprintf("%08x", crc32.ChecksumIEEE(b)), nil
|
2022-01-12 21:20:57 +00:00
|
|
|
case "blake2b":
|
|
|
|
return fmt.Sprintf("%x", blake2b.Sum256(b)), nil
|
|
|
|
case "blake2s":
|
|
|
|
return fmt.Sprintf("%x", blake2s.Sum256(b)), nil
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 22:26:33 +00:00
|
|
|
return "", fmt.Errorf("%w: %q", ErrHashMethodNotSupported, method)
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 10:09:08 +00:00
|
|
|
// WriteChecksums calculates the checksums for the given files using the specified hash methods,
|
|
|
|
// and writes the checksums to files named after the hash methods (e.g. "md5sum.txt", "sha256sum.txt").
|
|
|
|
func WriteChecksums(files, methods []string, outDir string) ([]string, error) {
|
|
|
|
if len(files) == 0 {
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
checksumFiles := make([]string, 0)
|
2016-08-26 08:38:22 +00:00
|
|
|
|
|
|
|
for _, method := range methods {
|
2024-05-07 10:09:08 +00:00
|
|
|
checksumFile := filepath.Join(outDir, method+"sum.txt")
|
|
|
|
|
|
|
|
f, err := os.Create(checksumFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2016-08-26 08:38:22 +00:00
|
|
|
for _, file := range files {
|
|
|
|
handle, err := os.Open(file)
|
|
|
|
if err != nil {
|
2024-01-04 22:26:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to read %q artifact: %w", file, err)
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
2024-05-07 10:09:08 +00:00
|
|
|
defer handle.Close()
|
2016-08-26 08:38:22 +00:00
|
|
|
|
2024-05-07 10:09:08 +00:00
|
|
|
hash, err := Checksum(handle, method)
|
2016-08-26 08:38:22 +00:00
|
|
|
if err != nil {
|
2024-01-04 22:26:33 +00:00
|
|
|
return nil, fmt.Errorf("could not checksum %q file: %w", file, err)
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 10:09:08 +00:00
|
|
|
_, err = f.WriteString(fmt.Sprintf("%s %s\n", hash, file))
|
|
|
|
if err != nil {
|
2016-08-26 08:38:22 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:09:08 +00:00
|
|
|
checksumFiles = append(checksumFiles, checksumFile)
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 10:09:08 +00:00
|
|
|
return append(files, checksumFiles...), nil
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|