forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
58 lines (46 loc) · 928 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package util
import (
"crypto/sha1"
"fmt"
"io"
"os"
)
//go:generate counterfeiter . Sha1Checksum
type Sha1Checksum interface {
ComputeFileSha1() ([]byte, error)
CheckSha1(string) bool
SetFilePath(string)
}
type sha1Checksum struct {
filepath string
}
func NewSha1Checksum(filepath string) Sha1Checksum {
return &sha1Checksum{
filepath: filepath,
}
}
func (c *sha1Checksum) ComputeFileSha1() ([]byte, error) {
hash := sha1.New()
f, err := os.Open(c.filepath)
if err != nil {
return []byte{}, err
}
defer f.Close()
if _, err := io.Copy(hash, f); err != nil {
return []byte{}, err
}
return hash.Sum(nil), nil
}
func (c *sha1Checksum) CheckSha1(targetSha1 string) bool {
sha1, err := c.ComputeFileSha1()
if err != nil {
return false
}
if fmt.Sprintf("%x", sha1) == targetSha1 {
return true
}
return false
}
func (c *sha1Checksum) SetFilePath(filepath string) {
c.filepath = filepath
}