-
Notifications
You must be signed in to change notification settings - Fork 2
/
transparency.go
91 lines (74 loc) · 2.42 KB
/
transparency.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
91
package sourceafis
import (
"fmt"
"sort"
"github.com/jtejido/sourceafis/features"
"github.com/jtejido/sourceafis/matcher"
"github.com/jtejido/sourceafis/transparency"
"github.com/fxamacker/cbor/v2"
)
type Transparency interface {
Accepts(key string) bool
Accept(key, mime string, data []byte) error
}
type DefaultTransparencyLogger struct {
spi Transparency
}
func NewTransparencyLogger(taker Transparency) *DefaultTransparencyLogger {
return &DefaultTransparencyLogger{
spi: taker,
}
}
func (t *DefaultTransparencyLogger) log(key, mime string, supplier func() ([]byte, error)) error {
// t.logVersion(); dont version this yet
if t.spi.Accepts(key) {
data, err := supplier()
if err != nil {
return err
}
t.spi.Accept(key, mime, data)
}
return nil
}
func (t *DefaultTransparencyLogger) Log(key string, data interface{}) error {
return t.log(key, "application/cbor", func() ([]byte, error) {
return cbor.Marshal(data)
})
}
func (t *DefaultTransparencyLogger) LogSkeleton(keyword string, skeleton *features.Skeleton) error {
return t.Log(skeleton.T.String()+keyword, transparency.NewConsistentSkeleton(skeleton))
}
func (t *DefaultTransparencyLogger) LogRootPairs(count int, roots []*matcher.MinutiaPair) error {
return t.Log("roots", transparency.Roots(count, roots))
}
func (t *DefaultTransparencyLogger) LogPairing(pairing *matcher.PairingGraph) error {
return t.Log("pairing", transparency.NewConsistentPairingGraph(pairing))
}
func (t *DefaultTransparencyLogger) LogBestPairing(pairing *matcher.PairingGraph) error {
return t.Log("best-pairing", transparency.NewConsistentPairingGraph(pairing))
}
func (t *DefaultTransparencyLogger) LogScore(score *matcher.ScoringData) error {
return t.Log("score", score)
}
func (t *DefaultTransparencyLogger) LogBestScore(score *matcher.ScoringData) error {
return t.Log("best-score", score)
}
func (t *DefaultTransparencyLogger) LogEdgeHash(hash map[int][]*features.IndexedEdge) error {
keys := make([]int, 0, len(hash))
for key := range hash {
keys = append(keys, key)
}
sort.Ints(keys)
entries := make([]*transparency.ConsistentHashEntry, 0, len(keys))
for _, key := range keys {
entries = append(entries, &transparency.ConsistentHashEntry{
Key: key,
Edges: hash[key],
})
}
return t.Log("edge-hash", entries)
}
func (t *DefaultTransparencyLogger) LogBestMatch(nth int) error {
t.spi.Accept("best-match", "text/plain", []byte(fmt.Sprintf("%d", nth)))
return nil
}