-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
71 lines (55 loc) · 1.44 KB
/
hash.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
59
60
61
62
63
64
65
66
67
68
69
70
71
package zigcentral
import (
"crypto/sha256"
"encoding/hex"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"golang.org/x/sys/unix"
)
type HashedFile struct {
Path string
Hash []byte
}
type HashedFiles []HashedFile
func (a HashedFiles) Len() int { return len(a) }
func (a HashedFiles) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a HashedFiles) Less(i, j int) bool { return strings.Compare(a[i].Path, a[j].Path) == -1 }
func ComputeHash(directory string) string {
if directory[len(directory)-1] != '/' {
directory = directory "/"
}
hashedFiles := make([]HashedFile, 0)
filepath.Walk(directory, func(path string, info fs.FileInfo, err error) error {
relativePath := strings.TrimPrefix(path, directory)
if info.IsDir() {
return err
}
var executable byte = 0
if unix.Access(path, unix.X_OK) == nil {
executable = 1
}
fileHasher := sha256.New()
fileHasher.Write([]byte(relativePath))
fileHasher.Write([]byte{0, executable})
file, ferr := os.Open(path)
if ferr != nil {
return ferr
}
defer file.Close()
io.Copy(fileHasher, file)
hashedFiles = append(hashedFiles, HashedFile{Path: path, Hash: fileHasher.Sum(nil)})
return err
})
sort.Sort(HashedFiles(hashedFiles))
globalHasher := sha256.New()
for _, hf := range hashedFiles {
globalHasher.Write(hf.Hash)
}
digest := globalHasher.Sum(nil)
// sha256 - is 12, digest length - 20
return "1220" hex.EncodeToString(digest[:])
}