56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package digest
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"hash"
|
|
"io"
|
|
)
|
|
|
|
const (
|
|
// SHA256DigestAlgorithm identifies the SHA-256 digest algorithm.
|
|
SHA256DigestAlgorithm = "sha256"
|
|
// SHA512DigestAlgorithm identifies the SHA-512 digest algorithm.
|
|
SHA512DigestAlgorithm = "sha512"
|
|
)
|
|
|
|
var (
|
|
errUnsupportedAlgorithm = fmt.Errorf("unsupported digest algorithm")
|
|
validDigestAlgorithms = [...]string{SHA256DigestAlgorithm, SHA512DigestAlgorithm}
|
|
)
|
|
|
|
// IsValidDigestAlgorithm returns true if the provided algorithm is supported
|
|
func IsValidDigestAlgorithm(alg string) bool {
|
|
for _, a := range validDigestAlgorithms {
|
|
if a == alg {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ValidDigestAlgorithms returns a list of supported digest algorithms
|
|
func ValidDigestAlgorithms() []string {
|
|
return validDigestAlgorithms[:]
|
|
}
|
|
|
|
// CalculateDigestWithAlgorithm computes the hex-encoded digest of r using the specified algorithm.
|
|
func CalculateDigestWithAlgorithm(r io.Reader, alg string) (string, error) {
|
|
var h hash.Hash
|
|
switch alg {
|
|
case SHA256DigestAlgorithm:
|
|
h = sha256.New()
|
|
case SHA512DigestAlgorithm:
|
|
h = sha512.New()
|
|
default:
|
|
return "", errUnsupportedAlgorithm
|
|
}
|
|
|
|
if _, err := io.Copy(h, r); err != nil {
|
|
return "", fmt.Errorf("failed to calculate digest: %v", err)
|
|
}
|
|
digest := h.Sum(nil)
|
|
return hex.EncodeToString(digest), nil
|
|
}
|