forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha.go
49 lines (38 loc) · 1020 Bytes
/
sha.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
package helpers
import (
"crypto/sha1"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"
. "github.com/onsi/gomega"
)
// Sha1Sum calculates the SHA1 sum of a file.
func Sha1Sum(path string) string {
f, err := os.Open(path)
Expect(err).ToNot(HaveOccurred())
defer f.Close()
hash := sha1.New()
_, err = io.Copy(hash, f)
Expect(err).ToNot(HaveOccurred())
return fmt.Sprintf("%x", hash.Sum(nil))
}
// Sha256Sum calculates the SHA256 sum of a file.
func Sha256Sum(path string) string {
f, err := os.Open(path)
Expect(err).ToNot(HaveOccurred())
defer f.Close()
hash := sha256.New()
_, err = io.Copy(hash, f)
Expect(err).ToNot(HaveOccurred())
return fmt.Sprintf("%x", hash.Sum(nil))
}
// Sha256SumDirectory calculates the SHA256 sum of a directory.
func Sha256SumDirectory(dir string) string {
tempZipFile, err := ioutil.TempFile(os.TempDir(), "")
Expect(err).ToNot(HaveOccurred())
err = Zipit(dir, tempZipFile.Name(), "")
Expect(err).ToNot(HaveOccurred())
return Sha256Sum(tempZipFile.Name())
}