-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfingerprint_root.go
90 lines (75 loc) · 1.68 KB
/
fingerprint_root.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
)
// RFing represents root fingerprint
type RFing struct {
Fingerprint string
FilesFingerprints []string
Changed bool // marks if the root fingerprint changed
}
// BuildRFing builds fingerprint for all provided files
func BuildRFing(ffings []FFing) (rfing RFing) {
var fings []string
for _, f := range ffings {
fings = append(fings, f.Fing)
}
// need to sort fingerprints to ensure correct root fingerprint
sort.Strings(fings)
hasher := sha256.New()
if _, err := hasher.Write([]byte(strings.Join(fings, ""))); err != nil {
panic(err)
}
fing := hex.EncodeToString(hasher.Sum(nil))
rfing = RFing{fing, fings, false}
return
}
// SaveRFing saves fingerprint in path
func SaveRFing(rfing RFing, path string) {
f, err := os.Create(path)
if err != nil {
panic(err)
}
defer f.Close()
if !FullFing {
rfing.FilesFingerprints = nil
}
bytes, err := json.MarshalIndent(rfing, "", " ")
if err != nil {
panic(err)
}
_, err = fmt.Fprintln(f, string(bytes))
if err != nil {
panic(err)
}
return
}
// ReadRFing reads fingerprint in path
func ReadRFing(path string) (rfing *RFing, err error) {
file, err := os.Open(path)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, err
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
if err := json.Unmarshal(byteValue, &rfing); err != nil {
return nil, err
}
return rfing, nil
}
// Compare compares root fingerprints
func Compare(oldRfing *RFing, newRfing *RFing) bool {
if oldRfing == nil {
return true
}
return oldRfing.Fingerprint != newRfing.Fingerprint
}