-
Notifications
You must be signed in to change notification settings - Fork 36
/
digest.go
58 lines (46 loc) · 1.29 KB
/
digest.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package crypto
import (
"fmt"
"io"
"strings"
"os"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type digestImpl struct {
algorithm Algorithm
digest string
}
func NewDigest(algorithm Algorithm, digest string) digestImpl {
return digestImpl{
algorithm: algorithm,
digest: strings.TrimPrefix(digest, algorithm.Name()+":"),
}
}
func (c digestImpl) Algorithm() Algorithm { return c.algorithm }
func (c digestImpl) String() string {
if c.algorithm.Name() == DigestAlgorithmSHA1.Name() {
return c.digest
}
return fmt.Sprintf("%s:%s", c.algorithm.Name(), c.digest)
}
func (c digestImpl) Verify(reader io.Reader) error {
computedDigest, err := c.Algorithm().CreateDigest(reader)
if err != nil {
return bosherr.WrapError(err, "Computing digest from stream")
}
if c.String() != computedDigest.String() {
return bosherr.Errorf("Expected stream to have digest '%s' but was '%s'", c.String(), computedDigest.String())
}
return nil
}
func (m digestImpl) VerifyFilePath(filePath string, fs boshsys.FileSystem) error {
file, err := fs.OpenFile(filePath, os.O_RDONLY, 0)
if err != nil {
return bosherr.WrapErrorf(err, "Calculating digest of '%s'", filePath)
}
defer func() {
_ = file.Close()
}()
return m.Verify(file)
}