-
Notifications
You must be signed in to change notification settings - Fork 162
/
digest_calculator.go
41 lines (35 loc) · 1.09 KB
/
digest_calculator.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
package crypto
import (
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"strings"
)
type DigestCalculator interface {
Calculate(string) (string, error)
CalculateString(string) string
}
type digestCalculator struct {
fs boshsys.FileSystem
algorithms []boshcrypto.Algorithm
}
func NewDigestCalculator(fs boshsys.FileSystem, algos []boshcrypto.Algorithm) DigestCalculator {
return digestCalculator{
fs: fs,
algorithms: algos,
}
}
func (c digestCalculator) Calculate(filePath string) (string, error) {
digest, err := boshcrypto.NewMultipleDigestFromPath(filePath, c.fs, c.algorithms)
if err != nil {
return "", bosherr.WrapErrorf(err, "Calculating digest for %s", filePath)
}
return digest.String(), nil
}
func (c digestCalculator) CalculateString(data string) string {
digest, err := boshcrypto.NewMultipleDigest(strings.NewReader(data), c.algorithms)
if err != nil {
panic("According to the docs sha1.Write will never return an error")
}
return digest.String()
}