-
Notifications
You must be signed in to change notification settings - Fork 26
/
checksum.go
42 lines (34 loc) · 1021 Bytes
/
checksum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cargo
import (
"strings"
)
// Checksum represents a checksum algorithm and hash pair formatted as
// algorithm:hash.
type Checksum string
// Algorithm returns the algorithm portion of the checksum string. If that
// portion is missing, it defaults to "sha256".
func (c Checksum) Algorithm() string {
algorithm, _, found := strings.Cut(string(c), ":")
if !found {
return "sha256"
}
return algorithm
}
// Hash returns the hexidecimal encoded hash portion of the checksum string.
func (c Checksum) Hash() string {
_, hash, found := strings.Cut(string(c), ":")
if !found {
hash = string(c)
}
return hash
}
// EqualTo returns true only when the given checksum algorithms and hashes
// match.
func (c Checksum) Match(o Checksum) bool {
return strings.EqualFold(c.Algorithm(), o.Algorithm()) && c.Hash() == o.Hash()
}
// EqualTo returns true only when the given checksum formatted string
// algorithms and hashes match.
func (c Checksum) MatchString(o string) bool {
return c.Match(Checksum(o))
}