-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
57 lines (48 loc) · 1.12 KB
/
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
package ezdeploy
import (
"crypto/md5"
"encoding/hex"
"github.com/karrick/godirwalk"
"sort"
"strings"
)
func checksumBytes(buf []byte) string {
h := md5.New()
h.Write(buf)
return hex.EncodeToString(h.Sum(nil))
}
func checksumFile(filename string) (checksum string, err error) {
h := md5.New()
if err = streamFile(h, filename); err != nil {
return
}
checksum = hex.EncodeToString(h.Sum(nil))
return
}
func checksumDir(dir string) (checksum string, err error) {
var filenames []string
if err = godirwalk.Walk(dir, &godirwalk.Options{
FollowSymbolicLinks: true,
Callback: func(filename string, entry *godirwalk.Dirent) error {
if entry.IsDir() && strings.HasPrefix(entry.Name(), ".") {
return godirwalk.SkipThis
}
if entry.IsRegular() && !strings.HasPrefix(entry.Name(), ".") {
filenames = append(filenames, filename)
}
return nil
},
}); err != nil {
return
}
sort.Strings(filenames)
h := md5.New()
for _, filename := range filenames {
if err = streamFile(h, filename); err != nil {
return
}
h.Write([]byte{'\r', '\n'})
}
checksum = hex.EncodeToString(h.Sum(nil))
return
}